我试图找到一个解决方案,但似乎没有工作。我试图将JSON文件转换为CSV文件。以下是JSON格式:
[
{
"entity_id": "9AEA-09FECFA87339",
"product": "SLF_PRED",
"managing_server_id": "334841-854A-ECC259F48439",
"ad_domain": "",
"folder_path": "HOME",
"ip_address_list": "10.21.111.226",
"mac_address_list": "00-21-51-00-RD-32",
"host_name": "PREMLAPTOP",
"isolation_status": "not_supported",
"capabilities": [
]
},
下面是我使用的代码:
import json
import csv
#read file
with open('test.json', 'r') as jsonfile:
data=jsonfile.read()
# parse file
jsonobj = json.loads(data)
def jsontocsv(input_json, output_path):
keylist = []
for key in jsonobj[0]:
keylist.append(key)
f = csv.writer(open(output_path, "w"))
f.writerow(keylist)
for record in jsonobj:
currentrecord = []
for key in keylist:
currentrecord.append(record[key])
f.writerow(currentrecord)
jsontocsv(jsonobj,'test.csv')
当我运行这个-什么也没发生
如果格式正确,它应该写一个名为"test.csv"到python脚本所在的文件夹