重写了
[Python] 纯文本查看 复制代码 import os
INPUT_DIR = '.' # csv存放文件夹
OUTPUT_JSON = 'output.json' # 输出json文件名
with open(OUTPUT_JSON, 'w', encoding='utf-8') as f:
f.write('{\n')
count = 0
csvs = [x for x in os.listdir(INPUT_DIR) if x[-4:] == '.csv']
for csv in csvs:
count += 1
with open(csv) as f:
lines = f.readlines()
with open(OUTPUT_JSON, 'a', encoding='utf-8') as f:
f.write(' "{}": {{\n'.format(count))
f.write(' "gender": "female",\n')
f.write(' "describe": "声音有点像陈一发",\n')
f.write(' "tensor": [\n')
for i in range(len(lines)):
line = lines[i]
f.write(' ' + line[:-1])
if i != len(lines) - 1:
f.write(',\n')
else:
f.write('\n')
f.write(' ]\n')
f.write(' }\n')
with open(OUTPUT_JSON, 'a', encoding='utf-8') as f:
f.write('}') |