关注深度学习入门pytharm用pycharmd读取excle表格然后导入json库 json库与ide在文件夹中是同级别 切为相对路径 最好在电脑先后文件后缀方便查看文档格式 类似excel.xlxs resule.json 该文档路径为E:\excel\PatientData2021.05-2024-03.xlxs 建立的json库位 resule.json 运行后会运行出json库显示 import pandas as pd import json # 假设你的Excel文件名为 data.xlsx file_path = r'E:\excel\PatientData2021.05-2024-03.xlxs' # 读取Excel文件 df = pd.read_excel(file_path) # 提取所需的列 # 假设列名分别是 'ID', 'age', 'gender', 'Diagnosis' df = df[['ID', 'age', 'gender(0-女,1-男)', 'IHD(缺血性心脏病)', 'DCM(扩张型心肌病)', 'VHD(心脏瓣膜病)', 'COPD-慢性阻塞性肺疾病', '门脉高压', 'RID(风湿免疫疾病)', '甲亢','肾功能不全']] # 转换为所需的格式 result = {} for index, row in df.iterrows(): id = str(row['ID']) age = row['age'] gender = row['gender(0-女,1-男)'] diagnosis = row[[ 'IHD(缺血性心脏病)', 'DCM(扩张型心肌病)', 'VHD(心脏瓣膜病)', 'COPD-慢性阻塞性肺疾病', '门脉高压', 'RID(风湿免疫疾病)', '甲亢','肾功能不全']].tolist() result[id] = [age, gender, diagnosis] # 转换为JSON格式 json_result = json.dumps(result, ensure_ascii=False, indent=4) # 保存为JSON文件 with open('json_files/result.json', 'w', encoding='utf-8') as f: f.write(json_result) print("转换完成并保存为result.json文件")
【无标题】
最新推荐文章于 2024-07-31 14:30:10 发布