Quick script to convert json data to csv

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""convert json to csv
Author: Stephen Emslie
"""

USAGE = """
    django-admin.py dumpdata myapp.mymodel > /path/to/in.json
    python %prog /path/to/in.json /path/to/out.csv

    or in one command:

    django-admin.py dumpdata myapp.mymodel | python %prog - /tmp/out.csv
"""

import csv
import sys
import tempfile
import simplejson
from optparse import OptionParser

if __name__ == '__main__':
    parser = OptionParser(usage=USAGE)
    options, args = parser.parse_args()
    path, outpath = args[0], args[1]
    if path == '-':
        source = sys.stdin
    else:
        source = open(path)
    s = source.read()
    objects = [object['fields'] for object in simplejson.loads(s)]
    writer = csv.DictWriter(open(outpath, 'w'), objects[0].keys())
    objects.insert(0, dict(zip(objects[0].keys(), objects[0].keys())))
    for d in objects:
      for key, value in d.items():
        if isinstance(value, basestring):
          d[key] = value.encode('utf8')
    writer.writerows(objects)

More like this

  1. MongoDB data dump by jose_lpa 1 year, 3 months ago
  2. MongoDB data load by jose_lpa 1 year, 3 months ago
  3. Database cleanup by skyjur 3 years, 8 months ago
  4. JSON encoding middleware by kcarnold 5 years, 10 months ago
  5. Custom model field to store dict object in database by rudyryk 4 years ago

Comments

(Forgotten your password?)