How to convert json to csv (excel).
In order to get data from json to csv you can use the script below:
import json
import csv
f = open('file.json')
data = json.load(f)
f.close()
f = csv.writer(open('file.csv', 'wb+'))
# use encode to convert non-ASCII characters
for item in data:
values = [ x.encode('utf8') for x in item['fields'].values() ]
f.writerow([item['pk'], item['model']] + values)
PS: To get excel format you can just open file.csv in redactor and save as xlsx or you can do the same action using google-drive.
Written by Alexander
Related protips
2 Responses
I'm completely new to Python,
This error is reported
values = [ x.encode('utf8') for x in item['fields'].values() ]
TypeError: string indices must be integers
over 1 year ago
·
You should consider looking at pandas for this stuff..
It's pretty cool
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#