How to iterate through JSON objects in Python and save to a CSV file?
I have Python script which retrieves data from an API in JSON format (at least I think its JSON format). I want to loop through each 'object' and write to a CSV file.
Here is the data returned from the api:
[
{
"bids_won": "7",
"partner_revenue": "$0.01",
"profit": "$0.00",
"campaign_id": "test 1",
"post_click_conversions": "0",
"total_cost": "$0.01",
"total_media_cost": "$0.01",
"post_view_conversions": "2",
"adjusted_partner_cost": "$0.01",
"clicks": "0",
"day": "August 21, 2011"
},
{
"bids_won": "30,209",
"partner_revenue": "$38.67",
"profit": "$8.92",
"campaign_id": "test 2",
"post_click_conversions": "0",
"total_cost": "$29.75",
"total_media_cost": "$29.75",
"post_view_conversions": "0",
"adjusted_开发者_如何学编程partner_cost ": "$25.26",
"clicks": "10",
"day": "August 21, 2011"
}
]
How can I loop through these 'objects' and write them to a CSV file? My current attempt to loop through it results in the script interating through each letter..
Appreciate the help.
ps I'm using python 2.7
To convert this to a proper csv file, with keys as a header row, something like this should do it, where "data" is the string containing your json:
import json
from csv import DictWriter
dicts = json.loads(data)
the_file = open("sample.csv", "w")
writer = DictWriter(the_file, dicts[0].keys())
writer.writeheader()
writer.writerows(dicts)
the_file.close()
Note that as of Python 2.6, simplejson has been included in the standard library as "json".
Try using simplejson
to convert the JSON string to a native Python object. You should then be able to loop through the objects using a standard for loop.
http://pypi.python.org/pypi/simplejson/
data = simplejson.loads(JSON_STRING)
This will do it:
import json
for i in json.loads(json_string):
print ','.join(i)
See json module docs
You should use simplejson
: http://pypi.python.org/pypi/simplejson/
With simplejson you can simply do:
import simplejson as json
info = """[
{
"bids_won": "7",
"partner_revenue": "$0.01",
"profit": "$0.00",
"campaign_id": "test 1",
"post_click_conversions": "0",
"total_cost": "$0.01",
"total_media_cost": "$0.01",
"post_view_conversions": "2",
"adjusted_partner_cost": "$0.01",
"clicks": "0",
"day": "August 21, 2011"
},
{
"bids_won": "30,209",
"partner_revenue": "$38.67",
"profit": "$8.92",
"campaign_id": "test 2",
"post_click_conversions": "0",
"total_cost": "$29.75",
"total_media_cost": "$29.75",
"post_view_conversions": "0",
"adjusted_partner_cost ": "$25.26",
"clicks": "10",
"day": "August 21, 2011"
}
]"""
data = json.loads(info)
And data
will be a python list that you can iterate through.
import json
import csv
temp = json.load(open('filename.json','r'))
output =[]
for each in temp:
row = {}
row['field1'] =each['field1']
row['field2'] = each['field2']
output.append(row)
file = open( "filename_destination.csv", "w")
fileWriter = csv.writer(file , delimiter=",",quotechar='"', quoting=csv.QUOTE_MINIMAL)
Header = ['field1','field2']
fileWriter.writerow(Header)
for x in output:
te = [x['field1'],x['field2']]
fileWriter.writerow(te)
file.close()
In the end I was given a slightly different URL for the request which returned raw CSV. I dumped the content into a CSV file:
f = open('report.csv', 'wb')
f.write(response_content)
f.close
Its not an ideal solution because I still cannot loop through the CSV objects (goes through each character) but I'm able to create a resonably well formatted csv file so its ok for now.
精彩评论