【发布时间】:2019-01-12 01:44:24
【问题描述】:
我在尝试清理 json 数据、将其保存到另一个 json 并最终将其转换为 csv 文件时遇到此错误: 对于 n 中的 data1:TypeError: 'int' object is not iterable
这是我的代码:
import json
import csv
with open('leap_data.json') as f:
data = json.load(f)
#print(data)
cnt=0
final_list={}
for k,v in data.items():
for m,n in v.items():
#print n
cnt=cnt+1
#print cnt
if cnt==6:
#print n
for data1 in n:
for a,b in data1.items():
final_list[a]=b
with open('output4.json', 'a') as outfile:
json.dump(final_list,outfile)
my_list = "["+open('output4.json').read().replace("}{","},{")+"]"
data_1 = json.loads(my_list)
print(data_1)
with open(r'samp.csv', 'w+') as csvfile:
output = csv.writer(csvfile)
output.writerow(data_1[0].keys())
for row in data_1:
output.writerow(row.values())
请建议如何摆脱这个
【问题讨论】:
-
n显然是一个整数。您的代码与输入数据不匹配。 -
但是这段代码在 Python 2 中运行
-
您应该将该信息添加到问题中。 json 或其他一些库可能在 2 和 3 之间发生了变化。
-
相同的代码在 Python 2 中运行良好。仅在 .item() 的情况下,我在 Python 2 中使用了 .iteritems()。
-
您还应该提供输入数据
标签: python json python-3.x python-2.7 typeerror