xdonta
Last Updated: December 01, 2017
·
17.73K
· cboji
Pycon logo

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.

Say Thanks
Respond

1 Response
Add your response

9097
Cfe62c6125255a57dafd3d23f743f3d9

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 ·