0

有人给我发了这段代码来从 json 转换为 csv 格式。

这是 json2csv 的代码。

import sys, json, csv

input = open(sys.argv[1])
json_array = json.load(input)
input.close()

item_data = json_array
if len(item_data) >= 1:
    first_item_id = item_data[0]['item_id']
    columns = item_data[0].keys()

csv_file = open(sys.argv[2], "wb")
writer = csv.writer(csv_file)
# there is currently a known bug where column names are partially uppercase, this    will  be fixed soon. the "map(lambda x: x.lower(), columns)" fixes this issue in the mean time
writer.writerow(map(lambda x: x.lower(), columns)) 

# here .items() is a standard python function
for item in item_data:
    row = []
    for column_name in columns:
        if column_name.lower() == 'name_part': # lower required due to above issue
            row.append(" ".join(item[column_name]))
        else:
            row.append(item[column_name])
    writer.writerow(row)

这是我的 json 数据。我保存为 transaction.json

{"comment": "Developer test ", "invoice_intern_external_ids": "", "invoice_payments": [{"payment_id": 8, "payment_method": "Refund", "timestamp": "2013-03-05", "invoice_id": 12, "writeoff_reason": "", "payment": 160.0}, {"payment_id": 9, "payment_method": "Cash", "timestamp": "2013-03-05", "invoice_id": 12, "writeoff_reason": "", "payment": 160.0}], "tax": 0.0, "pay_to_external_id": -1, "total": 0.0, "pay_to_contact_id": 13, "client_external_id": 11, "is_draft": false, "invoice_clinician_external_id": 999925, "location": "Therapy A", "invoice_clinician_id": 7, "bill_to_external_id": 11, "timestamp": "2013-03-05", "client_contact_id": 16, "subtotal": 0.0, "invoice_id": 26, "write_off": 0.0, "invoice_items": [{"item_tax": 0.0, "item_name": "InitialVisit_O", "timestamp": "2013-03-05", "item_unit_price": 160.0, "tax": 0.0, "invoice_item_id": 21, "invoice_instance_id": 26, "total": 0.0, "subtotal": 0.0, "item_description": "Initial Assessment/hour", "quantity": 0.0}], "billing_date": "2013-03-05", "invoice_intern_ids": "[]", "bill_to_contact_id": 16, "balance": 0.0, "invoice_instance_id": 12}
{"comment": "", "invoice_intern_external_ids": null, "invoice_payments": [], "tax": 0.0, "pay_to_external_id": -1, "total": 260.0, "pay_to_contact_id": 13, "client_external_id": -1, "is_draft": false, "invoice_clinician_external_id": null, "location": "Sports Medicine", "invoice_clinician_id": 7, "bill_to_external_id": -1, "timestamp": "2013-02-25", "client_contact_id": 15, "subtotal": 260.0, "invoice_id": 23, "write_off": 0.0, "invoice_items": [{"item_tax": 0.0, "item_name": "CompAsses", "timestamp": "2013-02-25", "item_unit_price": 260.0, "tax": 0.0, "invoice_item_id": 36, "invoice_instance_id": 23, "total": 260.0, "subtotal": 260.0, "item_description": "Comp Assess Report", "quantity": 1.0}], "billing_date": "2013-02-22", "invoice_intern_ids": "[]", "bill_to_contact_id": 15, "balance": 260.0, "invoice_instance_id": 10}

我试着做c:\python.exe c:\json2csv.py c:\transaction.json c:\transaction.txt 我得到了错误

Extra data line2 column 1 - line 12 column1 (char 1105 - char 11267)

如果有人可以更正代码以获取所有字段,那就太好了。我什至不需要csv中的所有字段。我只需要client_external_id, invoice_clinician_id, invoice_id, location, i tem_name, item_unit_price, item_description, quantity, billing_date.

这已经挂了很长时间了。我需要今天完成。请帮助。

4

1 回答 1

1

这里有多个问题:

  1. 您的 JSON 数据实际上是多个 JSON 数据。如果您有大量数据,这将很难解决,尽管 Martijns 建议每行读取可能会有所帮助,假设数据确实是每行一个 JSON 映射。否则需要修复数据,如下所示:

    [{"comment": "Developer test ", "invoice_intern_external_ids": "" ...},
     {"comment": "", "invoice_intern_external_ids": null, ...}]
    

    注意每个 JSON {} 之后的左括号和右括号以及逗号(最后一个除外)。

  2. 你得到的脚本不是特别通用。它假设在给定的第一个 JSON 对象中有一个“item_id”,但实际上没有。不过,这是可以解决的。

  3. 您的 invoice_payments 数据是一个字典列表。这意味着您的数据是分层的。你想如何转换为 CVS,它只是一个扁平的数据列表?这并不明显。您显示的脚本不处理该问题,它是通用的,并假设您的 json 数据是平坦的。

固定转换器:

import sys, json, csv

input = open(sys.argv[1])
json_array = []
for data in input.readlines():
     json_array.append(json.loads(data))
input.close()

item_data = json_array
if len(item_data) >= 1:
    columns = item_data[0].keys()

csv_file = open(sys.argv[2], "wb")
writer = csv.writer(csv_file)
# there is currently a known bug where column names are partially uppercase, this    will  be fixed soon. the "map(lambda x: x.lower(), columns)" fixes this issue in the mean time
writer.writerow(map(lambda x: x.lower(), columns)) 

# here .items() is a standard python function
for item in item_data:
    row = []
    for column_name in columns:
        if column_name.lower() == 'name_part': # lower required due to above issue
            row.append(" ".join(item[column_name]))
        else:
            row.append(item[column_name])
    writer.writerow(row)

将此结果生成为 CSV:

comment,invoice_intern_external_ids,invoice_payments,tax,pay_to_external_id,total,pay_to_contact_id,client_external_id,is_draft,invoice_clinician_external_id,location,invoice_instance_id,invoice_clinician_id,bill_to_external_id,timestamp,client_contact_id,subtotal,invoice_id,write_off,invoice_items,invoice_intern_ids,bill_to_contact_id,balance,billing_date
Developer test ,,"[{u'payment_id': 8, u'payment_method': u'Refund', u'invoice_id': 12, u'timestamp': u'2013-03-05', u'writeoff_reason': u'', u'payment': 160.0}, {u'payment_id': 9, u'payment_method': u'Cash', u'invoice_id': 12, u'timestamp': u'2013-03-05', u'writeoff_reason': u'', u'payment': 160.0}]",0.0,-1,0.0,13,11,False,999925,Therapy A,12,7,11,2013-03-05,16,0.0,26,0.0,"[{u'item_tax': 0.0, u'item_name': u'InitialVisit_O', u'timestamp': u'2013-03-05', u'item_unit_price': 160.0, u'tax': 0.0, u'subtotal': 0.0, u'invoice_item_id': 21, u'total': 0.0, u'invoice_instance_id': 26, u'item_description': u'Initial Assessment/hour', u'quantity': 0.0}]",[],16,0.0,2013-03-05
,,[],0.0,-1,260.0,13,-1,False,,Sports Medicine,10,7,-1,2013-02-25,15,260.0,23,0.0,"[{u'item_tax': 0.0, u'item_name': u'CompAsses', u'timestamp': u'2013-02-25', u'item_unit_price': 260.0, u'tax': 0.0, u'subtotal': 260.0, u'invoice_item_id': 36, u'total': 260.0, u'invoice_instance_id': 23, u'item_description': u'Comp Assess Report', u'quantity': 1.0}]",[],15,260.0,2013-02-22

请注意您的 invoice_payments 数据是如何转换为字符串的:

"[{u'payment_id': 8, u'payment_method': u'Refund', u'invoice_id': 12, u'timestamp': u'2013-03-05', u'writeoff_reason': u'', u'payment': 160.0}, {u'payment_id': 9, u'payment_method': u'Cash', u'invoice_id': 12, u'timestamp': u'2013-03-05', u'writeoff_reason': u'', u'payment': 160.0}]",0.0,-1,0.0,13,11,False,999925,Therapy A,12,7,11,2013-03-05,16,0.0,26,0.0,"[{u'item_tax': 0.0, u'item_name': u'InitialVisit_O', u'timestamp': u'2013-03-05', u'item_unit_price': 160.0, u'tax': 0.0, u'subtotal': 0.0, u'invoice_item_id': 21, u'total': 0.0, u'invoice_instance_id': 26, u'item_description': u'Initial Assessment/hour', u'quantity': 0.0}]"

导入 CSV 没有任何实际意义。您的 JSON 数据无法简单地转换为 CSV,您必须决定并指定 CSV 数据的外观。

于 2013-03-30T14:21:35.133 回答