【问题标题】:Converting Shopify API JSON to CSV将 Shopify API JSON 转换为 CSV
【发布时间】: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 中的关键字,也不是正确定义的一些值。

标签: python json shopify


【解决方案1】:

您的 json 文件包含一个 dict(在 json.load 之后), 使这一行 for x in x: 不必要,除非您的文件包含许多行,如您提供的那一行

您遇到的另一个问题是您访问x["variants"]["option1"] 的方式 因为x["variants"] 返回一个字典列表!所以你的 foreach 应该在那里:

for item in x["variants"]:
    item["option1"]
    #etc...

【讨论】:

  • @Nullman - 感谢您的推荐。我已经按照您的建议进行了操作,并且它已在“变体”中的字段上工作,尽管我无法从文件的第一行传递数据。有没有办法将它结合起来从同一行的嵌套数据和顶级数据中获取数据?
  • 我想通了 - 感谢您的指导。这是我现在用来编写的代码,它可以按预期完美运行 - 已作为解决方案添加。感谢您的时间和精力。
【解决方案2】:

根据建议 - 这是在这个问题上对我有用的代码:

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(["url", "sku", "parent_id", "title", "vendor", "product type", 
"tags", "body_html", "image"])

f.writerow([x["handle"], x["id"],
    "",
    x["title"],
    x["vendor"],
    x["product_type"],
    x["tags"],
    x["body_html"],
    x["image"]])

for item in x["variants"]:
    f.writerow(["",item["sku"], x["id"],x["title"],"","","","",
    item["option1"]])

【讨论】:

    【解决方案3】:

    我认为先解析 JSON 然后按如下方式使用 csv 编写器会更容易(请注意,我刚刚搜索了这个,因为我正在做类似的事情并且这个解决方案对我有用)。我正在粘贴我找到的网站上的示例,链接到下面的源代码:)

    import json
    import csv
    employee_parsed = json.loads(employee_data)
    emp_data = employee_parsed['employee_details']
    # open a file for writing
    employ_data = open('/tmp/EmployData.csv', 'w')
    # create the csv writer object
    csvwriter = csv.writer(employ_data)
    count = 0
    for emp in emp_data:
          if count == 0:
                 header = emp.keys()
                 csvwriter.writerow(header)
                 count += 1
          csvwriter.writerow(emp.values())
    employ_data.close()
    

    来源: http://blog.appliedinformaticsinc.com/how-to-parse-and-convert-json-to-csv-using-python/

    【讨论】:

      猜你喜欢
      • 2015-08-23
      • 2019-08-21
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多