import pandas as pd import json # 1. 读取 Excel 文件(假设列名为 question 和 answer) try: df = pd.read_excel("input.xlsx", usecols=["question", "answer"]) # 明确指定列 except Exception as e: print(f"读取文件失败: {str(e)}") exit() # 2. 转换为字典列表 result = [ {"question": str(row["question"]), "answer": str(row["answer"])} for _, row in df.iterrows() ] # 3. 写入 JSON 文件 with open("train_qa.json", "w", encoding="utf-8") as f: json.dump(result, f, ensure_ascii=False, indent=2) print("转换成功!")
excel文件有两列,循环读取文件两列赋值到字典列表。字典的有两个key,分别为question和answer。将最终结果输出到json文件
大霞上仙2025-03-20 15:02
相关推荐
Blossom.118几秒前
使用Python和OpenCV实现图像识别与目标检测chicpopoo2 分钟前
Python打卡DAY46灏瀚星空4 分钟前
Python数学可视化:3D参数曲面与隐式曲面绘制技术晨尘光6 分钟前
在Windows下编译出llama_cpp_python的DLL后,在虚拟环境中使用方法Java Fans22 分钟前
如何在Windows本机安装Python并确保与Python.NET兼容小邹子24 分钟前
抑郁症患者数据分析隐藏用户_y38 分钟前
基于PyCharm推送代码到github实践记录sss191s1 小时前
校招 Java 面试基础题目解析学习指南含新技术实操要点YYXZZ。。1 小时前
PyTorch——非线性激活(5)蹦蹦跳跳真可爱5891 小时前
Python----目标检测(YOLO简介)