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
相关推荐
费弗里30 分钟前
Python全栈应用开发利器Dash 3.x新版本介绍(3)dme.1 小时前
Javascript之DOM操作加油吧zkf1 小时前
AI大模型如何重塑软件开发流程?——结合目标检测的深度实践与代码示例t_hj1 小时前
python规划czhc11400756631 小时前
Linux 76 rsync悠悠小茉莉2 小时前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)全干engineer2 小时前
Spring Boot 实现主表+明细表 Excel 导出(EasyPOI 实战)Fireworkitte2 小时前
Java 中导出包含多个 Sheet 的 Excel 文件m0_625686552 小时前
day53Real_man3 小时前
告别 requirements.txt,拥抱 pyproject.toml和uv的现代Python工作流