- import json
- b=[]
- json_file_path = 'newtrain.json'
- with open ('train.json',encoding='utf-8') as json_file:
- for line in json_file.readlines():
- dict=json.loads(line)
- b.append(dict)
- with open(json_file_path,'w',encoding='utf-8') as file_obj:
- json.dump(b,file_obj,ensure_ascii=False)
其中encoding=‘utf-8’以及ensure_ascii=False都是为了防止中文文件转存乱码的问题