txt2json.py
923 Bytes
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
# Small tool to convert txt catalogs
# /!. OVERWRITES EXISTING OUTPUT FILE
import json
import datetime
input_txt_file = 'univ_all.txt'
output_json_file = 'univ_all.json'
LINES_TO_SKIP = 4
json_data = {
"columns": ['DATETIME'],
"data": []
}
with open(input_txt_file) as fp:
skipped_lines_count = 0
for line in fp:
if skipped_lines_count < LINES_TO_SKIP:
skipped_lines_count += 1
continue
input_datetime_string = line[0:20]
input_datetime = datetime.datetime.strptime(
input_datetime_string,
"%Y/%m/%d %H:%M:%S"
)
# output_datetime_string = input_datetime.strftime("%Y-%m-%dT%H:%M:%S")
output_datetime_string = input_datetime.isoformat()
json_data['data'].append([output_datetime_string])
with open(output_json_file, 'w') as fp:
json.dump(json_data, fp, sort_keys=True)
print("Done.")