Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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).

How can I fetch the JSON data from the URL?
How can I transform it to CSV?

Thank you

like image 515
c24b Avatar asked Nov 27 '25 03:11

c24b


1 Answers

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))
like image 171
Hugh Bothwell Avatar answered Nov 29 '25 16:11

Hugh Bothwell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!