누군가 json에서 csv 형식으로 변환하기 위해이 코드를 보냈습니다.json2cvs 형식에서 변환 할 때 오류가 발생했습니다.
다음은 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)
은 여기에 누군가가 가져 오는 코드를 수정할 수 있다면 나는 오류
Extra data line2 column 1 - line 12 column1 (char 1105 - char 11267)
있어 c:\python.exe c:\json2csv.py c:\transaction.json c:\transaction.txt
일을 시도 내가 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}
저장된 data.Which 내 JSON의 모든 분야는 훌륭합니다. CSV의 모든 필드가 필요하지 않습니다. 단지 client_external_id
, invoice_clinician_id
, invoice_id
, location
,, item_unit_price
, item_description
, quantity
, billing_date
만 있으면됩니다.
이것은 오랫동안 보류되어 왔습니다. 오늘 완료해야합니다. 도움이됩니다. 여러 문제가 여기에있다
입력 내용은 * 여러 * JSON 항목으로 구성되지만 코드는 단일 JSON 데이터 구조 만 처리 할 수 있습니다. 라인 당 입력 파일 *을 읽을 필요가있을 것입니다. –
어떻게해야합니까? –
보낸 코드가 JSON 파일 *의 데이터 구조와 일치하지 않습니다.거기에'item_id' 키가 없으며, 예를 들어'name_part'도 없습니다. JSON 구조도 * 중첩 *되어 CSV로 잘 변환되지 않습니다. –