-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_full_export.py
99 lines (66 loc) · 2.3 KB
/
csv_full_export.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import glob
import json
from datetime import datetime
from pprint import pprint
# Get the timestamp
def timestamp_to_datetime(timestamp):
# Make a copy so that we don't modify the original
timestamp_string = str(timestamp)
timestamp_string = timestamp_string.replace('Z', '000')
return datetime.strptime(timestamp_string, '%Y-%m-%dT%H:%M:%S.%f')
files = glob.glob('./export-*.json')
files.sort()
class CsvBuilder(object):
def __init__(self):
self.headers = []
self.rows = []
def register_column(self, col):
if str(col) not in self.headers:
self.headers.append(str(col))
def register_columns(self, cols):
for col in cols:
self.register_column(col)
def add_record_as_row(self, record):
for key in record.keys():
self.register_column(key)
row = []
for col in self.headers:
row.append(str(record[col]))
self.rows.append(row)
def generate_csv(self):
all_rows = []
all_rows.append(','.join(self.headers))
for row in self.rows:
all_rows.append(','.join(row))
return '\n'.join(all_rows)
csv = CsvBuilder()
for filename in files:
print("Processing %s" % filename)
with open(filename, 'r') as f:
jdata = f.read()
data = json.loads(jdata)
start = timestamp_to_datetime(data[0]['@timestamp'])
cols = ['timestamp']
for rec in data:
row_data = {}
for key in rec.keys():
row_data['timestamp'] = rec['@timestamp']
if type(rec[key]) is dict:
if 'x' in rec[key].keys():
cols.append(key+'_x')
cols.append(key+'_y')
cols.append(key+'_z')
row_data[key+'_x'] = str(rec[key]['x'])
row_data[key+'_y'] = str(rec[key]['y'])
row_data[key+'_z'] = str(rec[key]['z'])
csv.register_columns(cols)
csv.add_record_as_row(row_data)
with open('full-export.csv', 'w') as of:
of.write(csv.generate_csv())
# with open(newfilename, 'w') as of:
# of.write(csv.generate_csv())
# print('File %s starts at %s and ends at %s' % (
# filename,
# start.strftime('%I:%M %p UTC'),
# end.strftime('%I:%M %p UTC')
# ))