我有一个纬度/经度的JSON文件,我想要转换为CSV文件。我想用Python做到这一点。我已阅读/尝试了所有其他stackoverflow和谷歌搜索结果的建议。我已经设法创建了CSV并包含标题,但除此之外,愚蠢的东西开始发生。到目前为止,这是我的代码的工作部分:
import json, csv
x="""[
{"longitude":"-73.689070","latitude":"40.718000"},
{"longitude":"-73.688400","latitude":"40.715990"},
{"longitude":"-73.688340","latitude":"40.715790"},
{"longitude":"-73.688370","latitude":"40.715500"},
{"longitude":"-73.688490","latitude":"40.715030"},
{"longitude":"-73.688810","latitude":"40.714370"},
{"longitude":"-73.688980","latitude":"40.714080"},
{"longitude":"-73.689350","latitude":"40.713390"},
{"longitude":"-73.689530","latitude":"40.712800"},
{"longitude":"-73.689740","latitude":"40.712050"},
{"longitude":"-73.689820","latitude":"40.711810"},
{"longitude":"-73.689930","latitude":"40.711380"},
{"longitude":"-73.690110","latitude":"40.710710"}
]"""
x = json.loads(x)
f = csv.writer(open("test.csv", "wb+"))
f.writerow(["longitude", "latitude"])
这就是它崩溃的地方(“?”意味着我不知道该放什么。我已经尝试了各种各样的组合,我在寻找答案时发现了这些组合):
for ? in ?:
f.writerow([?[?],?[?]])
我通过this question获得了little_fish的答案。我可以看到我们的JSON示例略有不同,我假设这与我无法使其工作有关...
非常感谢任何帮助,如果需要,我很乐意提供澄清。仅供参考,我是Python的新手,所以如果您要使用行话,请尽可能清楚地解释。谢谢! (P.S.不确定它是否重要,但我使用的是IDLE)。
答案 0 :(得分:9)
我会使用csv.DictWriter,因为你正在处理dicts,这正是DictWriter
所针对的情况。
rows = json.loads(x)
with open('test.csv', 'wb+') as f:
dict_writer = csv.DictWriter(f, fieldnames=['longitude', 'latitude'])
dict_writer.writeheader()
dict_writer.writerows(rows)
修改强>
由于.writeheader()
方法仅在2.7中添加,因此您可以在旧版本中使用类似的内容:
rows = json.loads(x)
fieldnames = ['longitude', 'latitude']
with open('test.csv', 'wb+') as f:
dict_writer = csv.DictWriter(f, fieldnames=fieldnames)
dict_writer.writerow(dict(zip(fieldnames, fieldnames)))
dict_writer.writerows(rows)
答案 1 :(得分:2)
逐个循环遍历列表,并明确写出字段名称对于这种情况可能是最简单的。
for row in x:
f.writerow( [row['longitude'], row['latitude']] )
答案 2 :(得分:1)
这将帮助您迭代每个项目并将其写入您的csv文件:
import json, csv
x = """[
{"longitude":"-73.689070","latitide":"40.718000"},
{"longitude":"-73.688400","latitide":"40.715990"},
{"longitude":"-73.688340","latitide":"40.715790"},
{"longitude":"-73.688370","latitide":"40.715500"},
{"longitude":"-73.688490","latitide":"40.715030"},
{"longitude":"-73.688810","latitide":"40.714370"},
{"longitude":"-73.688980","latitide":"40.714080"},
{"longitude":"-73.689350","latitide":"40.713390"},
{"longitude":"-73.689530","latitide":"40.712800"},
{"longitude":"-73.689740","latitide":"40.712050"},
{"longitude":"-73.689820","latitide":"40.711810"},
{"longitude":"-73.689930","latitide":"40.711380"},
{"longitude":"-73.690110","latitide":"40.710710"}
]"""
jsoned = json.loads(x)
with open("test.csv", "wb+") as csv_file:
csv_writer = csv.writer(csv_file)
for i in jsoned:
csv_writer.writerow([i[u'longitude'],
i[u'latitide']])
请注意,您在原始帖子中拼错了纬度(latitide)。
答案 3 :(得分:1)
基于一些答案回答,这就像一个魅力!:
import json, csv
x="""[
{"longitude":"-73.689070","latitude":"40.718000"},
{"longitude":"-73.688400","latitude":"40.715990"},
{"longitude":"-73.688340","latitude":"40.715790"},
{"longitude":"-73.688370","latitude":"40.715500"},
{"longitude":"-73.688490","latitude":"40.715030"},
{"longitude":"-73.688810","latitude":"40.714370"},
{"longitude":"-73.688980","latitude":"40.714080"},
{"longitude":"-73.689350","latitude":"40.713390"},
{"longitude":"-73.689530","latitude":"40.712800"},
{"longitude":"-73.689740","latitude":"40.712050"},
{"longitude":"-73.689820","latitude":"40.711810"},
{"longitude":"-73.689930","latitude":"40.711380"},
{"longitude":"-73.690110","latitude":"40.710710"}
]"""
x = json.loads(x)
f = csv.writer(open("test.csv", "wb+"))
f.writerow(["longitude", "latitude"])
for row in x:
f.writerow( [row['longitude'], row['latitude']] )