11

我有一个纬度/经度的 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([?[?],?[?]])

我从little_fish对这个问题的回答中得到了上述信息。我可以看到我们的 JSON 示例略有不同,我假设这与我无法让它工作的原因有关......

任何帮助将不胜感激,如果需要,我很乐意提供澄清。仅供参考,我是 Python 新手,所以如果您要使用行话,请尽可能清楚地解释它。谢谢!(PS不确定是否重要,但我正在使用IDLE)。

4

4 回答 4

10

我会使用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)
于 2012-12-12T21:22:05.780 回答
2

一个一个地循环遍历列表,并明确地写出字段名称对于这种情况来说可能是最简单的。

for row in x:
    f.writerow( [row['longitude'], row['latitude']] )
于 2012-12-12T21:00:17.303 回答
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']])

请注意,您在原始帖子中拼错了纬度(纬度)。

于 2012-12-12T21:05:21.870 回答
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']] )
于 2012-12-12T21:20:30.100 回答