led/other/csv2json.py

23 lines
665 B
Python

#coding=utf-8
import csv
import json
# 文件路径
file_path = 'note_data.txt'
# 读取文件并转换为 JSON
notes_dict = {}
with open(file_path, mode='r', encoding='utf-8') as file:
reader = csv.DictReader(file, delimiter='\t')
for row in reader:
note, frequency, wave_length = row['note'], row['frequency'], row['wave_length']
notes_dict[note] = {'frequency': float(frequency), 'wave_length': float(wave_length)}
# 将数据转换为 JSON 并打印
json_data = json.dumps(notes_dict, indent=4)
print(json_data)
# 写入 JSON 文件
with open('note_data.json', 'w', encoding='utf-8') as json_file:
json_file.write(json_data)