import sys
import json
import csv
import chardet
input_file = "./tab_airspace_data.csv"
lines = ""
def detect_encoding(file_path):
with open(file_path, 'rb') as f:
result = chardet.detect(f.read())
return result['encoding']
encoding = detect_encoding(input_file)
with open(input_file, "r",encoding= encoding) as f:
reader = csv.reader(f)
keys = []
datas = []
index = 0
for row in reader:
if(index == 0):
keys = row
else:
datas.append(dict(zip(keys, row)))
index+=1
json_str = json.dumps(datas, ensure_ascii=False, indent=4)
result_data = json_str.replace(r'\"','').replace(r'\\N','').replace(r'\n','')
output_file = input_file.replace("csv", "json")
with open(output_file, "w", encoding="utf-8") as f:
f.write(result_data)
print("convert success")