forked from drugo76/json2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2csv.py
More file actions
74 lines (59 loc) · 1.31 KB
/
Copy pathjson2csv.py
File metadata and controls
74 lines (59 loc) · 1.31 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
from sys import argv
from os import path
from types import *
import argparse
import logging
import json
import csv
def main():
parser = argparse.ArgumentParser(
description='Convert json file to csv'
)
parser.add_argument(
'-i',
'--input_file',
dest='input_file',
default=None,
required=True,
help='Source json file (mandatory)'
)
parser.add_argument(
'-o',
'--output_file',
dest='output_file',
default=None,
required=True,
help='Destination csv file (mandatory)'
)
args = parser.parse_args()
input_file = args.input_file
output_file = args.output_file
json_data = []
data = None
write_header = True
item_keys = []
with open(input_file) as json_file:
json_data = json_file.read()
try:
data = json.loads(json_data)
except Exception, e:
raise e
with open(output_file, 'wb') as csv_file:
writer = csv.writer(csv_file)
for item in data:
item_values = []
for key in item:
if write_header:
item_keys.append(key)
value = item.get(key, '')
if type(value) is StringTypes:
item_values.append(value.encode('utf-8'))
else:
item_values.append(value)
if write_header:
writer.writerow(item_keys)
write_header = False
writer.writerow(item_values)
if __name__ == "__main__":
main()