【发布时间】:2018-01-25 14:22:17
【问题描述】:
希望我能就我的代码获得一些帮助。在过去的几天里,我一直在爬取该站点以尝试找到符合要求的响应,虽然我找到了一些很好的信息,但我仍在努力实现一个工作版本。
我的代码来自this thread。
正如标题所示,我正在尝试获取从 Shopify Webhook 接收的嵌套 JSON 文件,将其展平并将其转换为 CSV。
这是我的 JSON 文件:
{"id":788032119674292922,"title":"Example T-Shirt","body_html":null,"vendor":"Acme","product_type":"Shirts","created_at":null,"handle":"example-t-shirt","updated_at":null,"published_at":"2018-01-23T03:11:38-05:00","template_suffix":null,"published_scope":"global","tags":"mens t-shirt example","variants":[{"id":642667041472713922,"product_id":788032119674292922,"title":"","price":"19.99","sku":"example-shirt-s","position":0,"inventory_policy":"deny","compare_at_price":"24.99","fulfillment_service":"manual","inventory_management":null,"option1":"Small","option2":null,"option3":null,"created_at":null,"updated_at":null,"taxable":true,"barcode":null,"grams":200,"image_id":null,"inventory_quantity":75,"weight":200.0,"weight_unit":"g","inventory_item_id":null,"old_inventory_quantity":75,"requires_shipping":true},{"id":757650484644203962,"product_id":788032119674292922,"title":"","price":"19.99","sku":"example-shirt-m","position":0,"inventory_policy":"deny","compare_at_price":"24.99","fulfillment_service":"manual","inventory_management":"shopify","option1":"Medium","option2":null,"option3":null,"created_at":null,"updated_at":null,"taxable":true,"barcode":null,"grams":200,"image_id":null,"inventory_quantity":50,"weight":200.0,"weight_unit":"g","inventory_item_id":null,"old_inventory_quantity":50,"requires_shipping":true}],"options":[{"id":527050010214937811,"product_id":null,"name":"Title","position":1,"values":["Small","Medium"]}],"images":[{"id":539438707724640965,"product_id":788032119674292922,"position":0,"created_at":null,"updated_at":null,"width":323,"height":434,"src":"\/\/cdn.shopify.com\/s\/assets\/shopify_shirt-39bb555874ecaeed0a1170417d58bbcf792f7ceb56acfe758384f788710ba635.png","variant_ids":[]}],"image":null}
这是我的代码:
import csv
import json
with open('product.json') as file:
x = json.load(file)
print x
f = csv.writer(open("test.csv", "wb+"))
f.writerow(["id", "title", "option1", "grams"])
for x in x:
f.writerow([x["id"],
x["title"],
x["variants"]["option1"],
x["variants"]["grams"]])
运行此代码时,我收到以下错误:
Traceback (most recent call last):
File "parser5.py", line 17, in <module>
f.writerow([x["id"],
TypeError: string indices must be integers
我认为问题在于没有索引引用“[0]”这一事实,尽管我不确定如何正确分配密钥并且在所有尝试中都失败了。
我对 Python 还很陌生,我尝试阅读该网站上的尽可能多的帖子,以尽量不重复这个问题 - 如果这是徒劳的,我深表歉意。
我从文件中寻找的期望输出是这样的:
id,title,option1,grams
788032119674292922,Example T-Shirt, Small, 200
null,null,Medium,200
如果您能提供任何帮助或指导,我们将不胜感激。
【问题讨论】:
-
@Tim Silver,首先你需要更正你的 JSON 格式。 null 不是 python 中的关键字,也不是正确定义的一些值。