python 加载/保存json文件
说在前面
近日经常需要加载json文件数据来使用,经过处理后又需要保存为json文件,频繁操作容易忘记,故在此记录下简单的操作方法代码。
代码
import json
from datasets import tqdm
data_path = './results_final_sort.json'
with open(data_path, 'r', encoding='utf-8') as file:
predict_data = json.load(file)
results = []
for item in tqdm(predict_data):
id = item['id']
text = item['text']
triple = item['triple']
llm_output = item['llm_output']
llm_output_dict = json.loads(llm_output)
new_item = {
"id": id,
"text": text,
"triple": triple,
"llm_output": llm_output_dict
}
results.append(new_item)
json_results = json.dumps(results, ensure_ascii=False, indent=2)
write_path = './predict_data.json'
with open(write_path, 'w', encoding='utf-8') as f:
f.write(json_results)
print(f"vote_yes have been save to {write_path}")
原文地址:https://blog.csdn.net/weixin_45947938/article/details/144325508
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!