-
Notifications
You must be signed in to change notification settings - Fork 0
/
jcsv.py
71 lines (49 loc) · 1.46 KB
/
jcsv.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
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
import json
import argparse
import pandas as pd
parser = argparse.ArgumentParser()
args = parser.add_argument('--file', required=True)
filename = args.file
def is_primitive(value):
if type(value, int):
return True
elif type(value, float):
return True
elif type(value, str):
return True
elif type(value, bool):
return True
def get_flat_list(keyname, arr):
flat_list = {}
for index, val in enumerate(arr):
flat_list[f'{keyname}_{index}'] = val
return flat_list
def is_just_a_list(arr):
return is_primitive(arr[0])
def get_flat_mutli_dict(keyname, d):
flat_dict = {}
counter = 0
for key, value in d.items():
flat_dict[f'{keyname}_{key}_{counter}'] = value
counter += 1
return flat_dict
def get_flat_json(obj):
res = {}
for key, value in obj.items():
if is_primitive(value):
res[key] = value
elif type(value, dict):
res.update(value)
elif type(value, list):
if is_just_a_list(value):
res.update(get_flat_list(value))
else:
res.update(get_flat_multi_dict(value))
else:
raise Exception("Custom classes are not supported!")
df = pd.DataFrame(res)
df.to_csv(f'{filename[:-5].csv', index=False)
if __name__ == '__main__':
with open(filename, 'r') as f:
json_file = json.loads(filename)
get_flat_json(json_file)