txt2json.py 923 Bytes

# 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.")