-
Notifications
You must be signed in to change notification settings - Fork 4
/
JSON2TSV.py
44 lines (35 loc) · 1.07 KB
/
JSON2TSV.py
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
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# -*- coding: latin-1 -*-
''' Script to convert a JSON file to TSV. Adapted from http://kailaspatil.blogspot.com/2013/07/python-script-to-convert-json-file-into.html
'''
import fileinput
import json
import csv
import sys
EPILOG = '''Usage: %(prog)s -i [input JSON file] > [output TSV file]'''
def main():
import argparse
parser = argparse.ArgumentParser(description=__doc__, epilog=EPILOG,
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument('--infile', '-i', help="JSON file to convert to TSV")
args = parser.parse_args()
lines = []
if args.infile:
with open(args.infile, 'r') as f:
for line in f:
lines.append(line)
else:
print >> sys.stderr, "Usage: JSON2TSV.py -i [input JSON file] > [output TSV file]"
sys.exit(0)
new_json = json.loads(''.join(lines))
keys = {}
for i in new_json:
for k in i.keys():
keys[k] = 1
tab_out = csv.DictWriter(sys.stdout, fieldnames=keys.keys(), dialect='excel-tab')
tab_out.writeheader()
for row in new_json:
tab_out.writerow(row)
if __name__ == '__main__':
main()