我在python中为我编写了一个代码json2csv文件(我将其命名为json2csv.py)。
import sys, json, csv
input = open(sys.argv[1])
json_array = json.load(input)
input.close()
item_data = json_array["items"]
if len(item_data) >= 1:
first_item_id = item_data[0].keys()[0]
columns = item_data[0][first_item_id].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_id, item_data in item_data[0].items():
row = []
for column_name in columns:
if column_name.lower() == 'name_part': # lower required due to above issue
row.append(" ".join(item_data[column_name]))
else:
row.append(item_data[column_name])
writer.writerow(row)
csv_file.close()
我的输入存储在pd.json文件中.pd.json的内容是
{"status": "ok", "items": [{"work_phone_extension": null, "name_part": ["PATIENT", "TEST"], "residential_street_address_line_2": "", "referring_physician_first_name": "", "residential_street_address_line_1": "", "work_phone": "", "referring_physician_last_name": "", "residential_postal_or_zip_code": "", "referring_physician_code": "", "residence_phone": "416-", "health_card": "", "item_id": 1, "unique_vendor_id_sequence": 1, "cell_phone": null, "residential_city": "Toronto", "health_card_version": "", "residential_country_and_province_or_state": "CA_ON"}, {"work_phone_extension": null, "name_part": ["STEVE", "TEST"], "residential_street_address_line_2": "", "referring_physician_first_name": "Adam", "residential_street_address_line_1": "123 Fake St", "work_phone": "", "referring_physician_last_name": "Test", "residential_postal_or_zip_code": "M5E1Z8", "referring_physician_code": "123456", "residence_phone": "416-555-5555", "health_card": "", "item_id": 2, "unique_vendor_id_sequence": 2, "cell_phone": null, "residential_city": "Toronto", "health_card_version": "", "residential_country_and_province_or_state": "CA_ON"}]}
我正在执行命令
c:\python27\python.exe c:\Python27\Scripts\json2csv.py c:\Python27\Scripts\pd.json c:\Python27\Scripts\pd.txt
我一直收到错误
'Nonetype' object has no attribute keys.
有人可以在第一行_item_id = item_data [0]指出json2csv中的错误.keys()[0]
我厌倦了坐下来处理这段代码。
此代码接收pd.json并将文件输出为pd.txt
答案 0 :(得分:1)
您的错误应位于columns = item_data[0][first_item_id].keys()
行,问题出在first_item_id
,我认为您应该更改此内容:
first_item_id = item_data[0].keys()[0]
到此:
first_item_id = item_data[0]['item_id']
但它仍有一些问题,我认为代码不是为这个json结构编写的,我只是改变了另一行来修复它,我不确定这是正确的解决方案,但这是来自于您当前的代码:
import sys, json, csv
input = open(sys.argv[1])
json_array = json.load(input)
input.close()
item_data = json_array["items"]
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)
csv_file.close()