训练模型完成后得到的json文件,如何转化为txt文件,代码如下:
# 把json文件转化为txt文件,可导入origin进行曲线绘图
import json
# Open the JSON file for reading
with open(r'...\scalars.json', 'r') as json_file: # 输入自己的json文件路径,用绝对路径不易报错
# Read the content of the JSON file
json_content = json_file.read()
# Split the content by newline to handle multiple JSON objects
json_objects = json_content.strip().split('\n')
# Open a new text file for writing
with open(r'...\json_as_excel.txt', 'w') as text_file: # 指定txt文件保存路径
# Write each JSON object as text to the text file
for json_obj in json_objects:
parsed_json = json.loads(json_obj)
text_file.write(json.dumps(parsed_json, indent=4) + '\n')
print("txt文件转化已完成.")