我有一个JSON文件,如下所示:
{
"resultSet": {
"totalRecords": "2",
"agentLogins": [
{
"phoneNumber": "99999",
"callerId": "999999999",
"stationId": "99999999",
"stationName": "",
"loginDate": "2020-09-17T15:25:34.410Z"
},
{
"phoneNumber": "99999",
"callerId": "99999999",
"stationId": "999999",
"stationName": "",
"loginDate": "2020-09-17T17:22:48.613Z"
}
]
}
}
我正在尝试使用Python将其转换为CSV文件,以下是我的代码:
import json
import csv
with open('agentLogins.json') as json_file:
data = json.load(json_file)
agentLogins = data['agentLogins']
data_file = open('agentLogins.csv', 'w')
csv_writer = csv.writer(data_file)
count = 0
for emp in agentLogins:
if count == 0:
header = emp.keys()
csv_writer.writerow(header)
count += 1
csv_writer.writerow(emp.values())
data_file.close()
I am getting an error: agentLogins = data['agentLogins'] KeyError: 'agentLogins:'.
If I Remove the first part it works:
"resultSet": {
"totalRecords": "2",
我只是想知道我是否可以在不移除任何东西的情况下也让它工作
将
data['agentLogins']
转换为data['resultSet']['agentLogins']
即可,这是解决方案:相关问题 更多 >
编程相关推荐