简体   繁体   English

下载JSON数据并使用Python将其转换为CSV

[英]Download JSON data and convert it to CSV using Python

I'm currently using Yahoo Pipes which provides me with a JSON file from an URL. 我目前正在使用Yahoo Pipes,它通过URL为我提供了JSON文件。

I would like to be able to fetch it and convert it into a CSV file, and I have no idea where to begin (I'm a complete beginner in Python). 我希望能够获取它并将其转换为CSV文件,而且我不知道从哪里开始(我是Python的完整初学者)。

How can I fetch the JSON data from the URL? 如何从URL提取JSON数据?
How can I transform it to CSV? 如何将其转换为CSV?

Thank you 谢谢

import urllib2
import json
import csv

def getRows(data):
    # ?? this totally depends on what's in your data
    return []

url = "http://www.yahoo.com/something"
data = urllib2.urlopen(url).read()
data = json.loads(data)

fname = "mydata.csv"
with open(fname,'wb') as outf:
    outcsv = csv.writer(outf)
    outcsv.writerows(getRows(data))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM