使用Python(空闲)将JSON转换为CSV [英] Convert JSON to CSV using Python (Idle)

查看:190
本文介绍了使用Python(空闲)将JSON转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要转换为CSV档案的「经度/纬度」JSON档案。我想使用Python做到这一点。我已经读/尝试所有其他stackoverflow和谷歌搜索结果建议。我已经设法得到远至创建CSV和包括标题,但除此之外,愚蠢的东西开始发生。到目前为止,我的代码的工作部分:

I have a JSON file of Latitude/Longitude that I want to covert to a CSV file. I want to do this using Python. I have read/tried all other stackoverflow and google search results suggestions. I've managed to get as far as creating the CSV and including headers, but beyond that, goofy stuff starts to happen. Here is the working part of my code so far:

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"])

这里是它分开的地方(?的意思是我不知道什么放在那里。我试过各种组合的东西,我发现在我找到的答案):

And here's where it falls apart ("?'s" mean I'm not sure what to put there. I've tried all sorts of combinations of things that I've found in my search for answers):

for ? in ?:
    f.writerow([?[?],?[?]])

我通过此问题的答案获得了上述内容href =http://stackoverflow.com/users/227684/little-fish> little_fish 。我可以看到我们的JSON示例略有不同,我假设这与为什么我不能让它工作...

I got the above from answers to this question by little_fish. I can see that our JSON examples are slightly different, and I am assuming that has something to do with why I can't get it to work...

任何帮助将非常感谢,我很乐意提供澄清如果需要。 FYI,我是Python的新手,所以如果你要使用行话,请尽可能清楚地解释。谢谢! (PS不确定是否重要,但我使用IDLE)。

Any help would be greatly appreciated, and I am happy to provide clarification if need be. FYI, I am new to Python, so if you're going to use jargon, please explain it as clearly as possible. Thanks! (P.S. Not sure if it matters, but I am using IDLE).

推荐答案

我会使用 csv.DictWriter ,因为你处理的是病例,这是情况 DictWriter 是为了。

I would use a csv.DictWriter, since you're dealing with dicts, which is exactly the case DictWriter is there for.

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中添加,因此您可以在旧版本上使用类似的方法: / p>



Since the .writeheader() method was only added in 2.7, you can use something like this on older versions:

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)

这篇关于使用Python(空闲)将JSON转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆