diff --git a/体测单位/南京化工.ipynb b/体测单位/南京化工.ipynb index 85bb539..785faa6 100644 --- a/体测单位/南京化工.ipynb +++ b/体测单位/南京化工.ipynb @@ -241,16 +241,9 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": null, "id": "def3f47a-24ef-4815-b63b-2a16b79b4c15", "metadata": { - "execution": { - "iopub.execute_input": "2025-08-13T08:03:28.281779Z", - "iopub.status.busy": "2025-08-13T08:03:28.281182Z", - "iopub.status.idle": "2025-08-13T08:03:28.305144Z", - "shell.execute_reply": "2025-08-13T08:03:28.304322Z", - "shell.execute_reply.started": "2025-08-13T08:03:28.281726Z" - }, "tags": [] }, "outputs": [], @@ -304,17 +297,9 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "id": "8b7c0528-17b6-4469-b15f-3c4b794e286e", - "metadata": { - "execution": { - "iopub.execute_input": "2025-08-13T08:04:18.256439Z", - "iopub.status.busy": "2025-08-13T08:04:18.255730Z", - "iopub.status.idle": "2025-08-13T08:04:18.280866Z", - "shell.execute_reply": "2025-08-13T08:04:18.280162Z", - "shell.execute_reply.started": "2025-08-13T08:04:18.256379Z" - } - }, + "metadata": {}, "outputs": [], "source": [ "import json\n", @@ -440,6 +425,14 @@ " json.dump(dict1, fl, ensure_ascii=False)\n" ] }, + { + "cell_type": "markdown", + "id": "4930b4cb-2114-4432-a8ae-e6d2cde69b5c", + "metadata": {}, + "source": [ + "## 导入问卷信息(新)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -453,22 +446,14 @@ "import time\n", "from datetime import date\n", "\n", - "dict1 = {}\n", "\n", - "filename = 'data/result_南京化工.json'\n", - "with open(filename,'r') as fl:\n", - " dict1 = json.load(fl)\n", "filename = 'data/南京化工人员.json'\n", "with open(filename,'r') as fl:\n", " dict3 = json.load(fl)\n", "\n", - "phone = {}\n", - "for k,v in dict3.items():\n", - " if 'phone' in v.keys():\n", - " phone[v['phone']] = k\n", "\n", "list1 = []\n", - "filename = 'data/survey_records_20250812.csv'\n", + "filename = 'data/survey_records_20250814.csv'\n", "with open(filename,'r',newline='') as csv_file:\n", " fl = csv.reader(csv_file,delimiter=',')\n", " header = next(fl) \n", @@ -476,44 +461,35 @@ " list1.append(line)\n", "\n", "\n", - "\n", + "dict1 = {}\n", "nn = 0\n", - "for item in list1:\n", - " \n", - " tcm = []\n", - " \n", - " \n", + "for item in list1: \n", + " tcm = [] \n", " for i in range(0,60):\n", " tcm.append(0)\n", " \n", - " content = json.loads(item[4])\n", - " if int(content['phone']) in phone.keys(): \n", - " code = phone[int(content['phone'])]\n", - " print(code)\n", - " if code not in dict1.keys():\n", - " dict1[code] = dict3[code]\n", - " rq = date.fromisoformat(item[5].replace('/','-').split(' ')[0])\n", - " dict1[code]['rq'] = str(date.fromisoformat(item[5].replace('/','-').split(' ')[0]))\n", - " #dict1[code]['rq'] = item[5].replace('/','-').split(' ')[0]\n", - " else:\n", - " rq=date.fromisoformat('2025-08-12')\n", - " dict1[code]['rq'] = '2025-08-12'\n", - " for k, v in content.items():\n", - " \n", - " if 'tcm' in k:\n", - " i = int(k[3:])\n", - " tcm[i-1] = int(v) \n", - " \n", - " if 'tcm' in item[4]: \n", - " dict1[code]['tcm'] = tcm\n", - " \n", - " birth = date.fromisoformat(dict3[code]['birth'].replace('/','-'))\n", - " \n", - " days = (rq-birth).days \n", - " dict1[code]['age'] = int(days/365)\n", - " dict1[code]['month'] = int(days/365*12)\n", - " #print(phone[item[2]])\n", - " nn+=1\n", + " content = json.loads(item[4]) \n", + " phone = content['phone']\n", + " dict1.setdefault(phone,{})\n", + " \n", + " #rq = date.fromisoformat(item[5].replace('/','-').split(' ')[0])\n", + " #dict1[phone]['rq'] = str(date.fromisoformat(item[5].replace('/','-').split(' ')[0]))\n", + " \n", + " for k, v in content.items(): \n", + " if 'tcm' in k:\n", + " i = int(k[3:])\n", + " tcm[i-1] = int(v) \n", + " \n", + " if 'tcm' in item[4]: \n", + " dict1[phone]['tcm'] = tcm\n", + " \n", + " dict1[phone]['name'] = content['name']\n", + " dict1[phone]['sex'] = content['gender']\n", + " dict1[phone]['weight'] = content['weight']\n", + " dict1[phone]['tun'] = content['hip']\n", + " dict1[phone]['yao'] = content['waist']\n", + " #print(phone[item[2]])\n", + " nn+=1\n", "filename = 'data/result_南京化工-2.json'\n", "\n", "with open(filename,'w') as fl:\n", @@ -563,6 +539,178 @@ " json.dump(dict1, fl, ensure_ascii=False)\n" ] }, + { + "cell_type": "markdown", + "id": "19c6e8b6-2663-447d-884b-a22af3944c71", + "metadata": { + "jp-MarkdownHeadingCollapsed": true + }, + "source": [ + "## 计算中医体质并导出" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e233e6c3-9c3f-42c7-8057-012bbcfe8b26", + "metadata": {}, + "outputs": [], + "source": [ + "import openpyxl\n", + "import json\n", + "\n", + "questions = [\n", + " [1],\n", + " [-1, 2],\n", + " [-1, 2],\n", + " [-1, 8],\n", + " [-1, 3],\n", + " [1],\n", + " [-1],\n", + " [-1, 7],\n", + " [2],\n", + " [2],\n", + " [2],\n", + " [2, 3],\n", + " [2],\n", + " [2],\n", + " [3],\n", + " [3],\n", + " [3],\n", + " [3],\n", + " [3],\n", + " [4],\n", + " [4],\n", + " [4],\n", + " [4],\n", + " [4],\n", + " [4],\n", + " [4],\n", + " [4],\n", + " [5],\n", + " [5],\n", + " [5],\n", + " [5],\n", + " [5],\n", + " [5],\n", + " [5],\n", + " [5],\n", + " [6],\n", + " [6],\n", + " [6],\n", + " [6],\n", + " [6],\n", + " [6],\n", + " [7],\n", + " [7],\n", + " [7],\n", + " [7],\n", + " [7],\n", + " [7],\n", + " [8],\n", + " [8],\n", + " [8],\n", + " [8],\n", + " [8],\n", + " [8],\n", + " [9],\n", + " [9],\n", + " [9],\n", + " [9],\n", + " [9],\n", + " [9],\n", + " [9]\n", + "]\n", + "\n", + "kinds = [\n", + " '平和',\n", + " '气虚',\n", + " '阳虚',\n", + " '阴虚',\n", + " '痰湿',\n", + " '湿热',\n", + " '血瘀',\n", + " '气郁',\n", + " '特禀'\n", + "]\n", + "\n", + "def tcm_calc(arr):\n", + " qa = [8, 8, 7, 8, 8, 6, 7, 7, 7]\n", + " # 成绩数组\n", + " s = [0] * 9\n", + " # 遍历五进制\n", + " for i in range(len(questions)):\n", + " m = arr[i] - 1\n", + " for v in questions[i]:\n", + " if v < 0:\n", + " s[-v - 1] += 4 - m\n", + " else:\n", + " s[v - 1] += m\n", + " return [int((v / qa[i]) * 25) for i, v in enumerate(s)]\n", + "\n", + "def tcm_kind(score):\n", + " kind = 0\n", + " near = False\n", + " max_kind = 0\n", + " max_score = 0\n", + " for i in range(1, 9):\n", + " if score[i] > max_score:\n", + " max_kind = i\n", + " max_score = score[i]\n", + " if score[0] >= 60 and max_score < 40:\n", + " if max_score >= 30:\n", + " near = True\n", + " kind = max_kind\n", + " else:\n", + " kind = max_kind\n", + " return {\n", + " \"kind\": kind,\n", + " \"near\": near\n", + " }\n", + "\n", + "\n", + "filename = 'data/result_南京化工-2.json'\n", + "with open(filename,'r') as fl:\n", + " dict1 = json.load(fl) \n", + "i = 1\n", + "list2 = []\n", + "for k, v in dict1.items():\n", + " if 'tcm' in v.keys():\n", + " list1 = []\n", + " tcm =v['tcm']\n", + " for item in tcm:\n", + " list1.append(item)\n", + " score = tcm_calc(list1)\n", + "\n", + " result = tcm_kind(score)\n", + " kind = result['kind']\n", + " near = result['near']\n", + " #print(i,k,kinds[kind], near, score)\n", + " #i+=1\n", + " list3 = []\n", + " list3.append(k)\n", + " list3.append(v['name'])\n", + " list3.append(v['sex'])\n", + " list3.append(v['weight'])\n", + " list3.append(v['yao'])\n", + " list3.append(v['tun'])\n", + " list3.append(kinds[kind])\n", + " list3.append(near)\n", + " for item in score:\n", + " list3.append(item)\n", + " list2.append(list3)\n", + "\n", + "filename = 'data/南化第二次问卷明细表.xlsx'\n", + "wb = openpyxl.Workbook()\n", + "sheet = wb.active\n", + "\n", + "for row in list2:\n", + " sheet.append(row)\n", + " \n", + "wb.save(filename)\n", + "print('ok') " + ] + }, { "cell_type": "markdown", "id": "caf77c95-3090-4fa1-bcec-c9e8a38d4ca9", @@ -729,10 +877,190 @@ "print(i)" ] }, + { + "cell_type": "markdown", + "id": "42f1a67c-756a-4cf0-bd53-d71fb9c95aa6", + "metadata": {}, + "source": [ + "## 导入体检报告数据" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "c2fb2c7c-6541-4c41-ba38-0e1fee29aa97", + "metadata": { + "execution": { + "iopub.execute_input": "2025-08-14T11:18:53.769605Z", + "iopub.status.busy": "2025-08-14T11:18:53.769133Z", + "iopub.status.idle": "2025-08-14T11:18:53.803613Z", + "shell.execute_reply": "2025-08-14T11:18:53.803014Z", + "shell.execute_reply.started": "2025-08-14T11:18:53.769567Z" + } + }, + "outputs": [], + "source": [ + "from pathlib import Path\n", + "import json\n", + "import shutil\n", + "\n", + "\n", + "target_directory = Path('./data/json')\n", + "filename = 'data/南京化工人员.json'\n", + "with open(filename,'r') as fl:\n", + " dict1 = json.load(fl)\n", + "# 遍历目标目录及其子目录获取所有文件\n", + "dict2 = {}\n", + "list2 = ['总胆固醇','甘油三酯','尿微量白蛋白']\n", + "for fl in target_directory.glob('*.json'):\n", + " if fl.is_file():\n", + " code = fl.stem\n", + " dict2.setdefault(code,{})\n", + " dict2[code] = dict1[code]\n", + " with open(fl,'r') as fl1:\n", + " dict3 = json.load(fl1)\n", + " for k, v in dict3.items():\n", + " if k =='血压情况' and len(v)>0:\n", + " dict2[code].setdefault('血压',{})\n", + " list1 = [] \n", + " for item in v:\n", + " \n", + " dict2[code]['血压'][item['项目']] = item['结果']\n", + " if '状态' in item.keys():\n", + " list1.append(item['项目']+item['状态'])\n", + " if len(list1)>0:\n", + " dict2[code]['血压']['状态'] = ','.join(list1)\n", + " \n", + " if k in list2:\n", + " dict2[code].setdefault(k,{})\n", + " dict2[code][k]['结果'] = v['结果']\n", + " dict2[code][k]['参考值'] = v['参考值']\n", + " if '状态' in v.keys():\n", + " dict2[code][k]['状态'] = v['状态']\n", + " if k in ['空腹血糖','糖化血红蛋白']:\n", + " dict2[code].setdefault(k,{})\n", + " if '结果' in v.keys():\n", + " dict2[code][k]['结果'] = v['结果']\n", + " dict2[code][k]['参考值'] = v['参考值']\n", + " if '状态' in v.keys():\n", + " dict2[code][k]['状态'] = v['状态']\n", + " if k in ['ALT、AST、GGT','TSH、FT3、FT4']:\n", + " for item in v:\n", + " xm = item['项目']\n", + " dict2[code].setdefault(xm,{})\n", + " if '结果' in item.keys():\n", + " dict2[code][xm]['结果'] = item['结果']\n", + " if '参考值' in item.keys():\n", + " dict2[code][xm]['参考值'] = item['参考值']\n", + " if '状态' in item.keys():\n", + " dict2[code][xm]['状态'] = item['状态']\n", + " if k =='肾功能与尿微量白蛋白':\n", + " for item in v['肾功能']:\n", + " xm = item['项目']\n", + " dict2[code].setdefault(xm,{})\n", + " if '结果' in item.keys():\n", + " dict2[code][xm]['结果'] = item['结果']\n", + " if '参考值' in item.keys():\n", + " dict2[code][xm]['参考值'] = item['参考值']\n", + " if '状态' in item.keys():\n", + " dict2[code][xm]['状态'] = item['状态'] \n", + " dict2[code].setdefault('尿微量白蛋白',{})\n", + " xm = v['尿微量白蛋白']\n", + " if '结果' in xm.keys() and len(xm['结果'])>0:\n", + " dict2[code]['尿微量白蛋白']['结果'] = xm['结果']\n", + " if '参考值' in xm.keys() and len(xm['参考值'])>0:\n", + " dict2[code]['尿微量白蛋白']['参考值'] = xm['参考值']\n", + " if '状态' in xm.keys():\n", + " dict2[code]['尿微量白蛋白']['状态'] = xm['状态'] \n", + " \n", + " \n", + "\n", + "filename = 'data/南京化工体检情况.json'\n", + "\n", + "with open(filename,'w') as fl:\n", + " json.dump(dict2, fl, ensure_ascii=False) " + ] + }, + { + "cell_type": "markdown", + "id": "da43ceef-4215-4187-a76d-4e1434e50043", + "metadata": {}, + "source": [ + "## 导出体检报告数据" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "daa1b36e-1b62-49ee-9539-84c2d286fefe", + "metadata": { + "execution": { + "iopub.execute_input": "2025-08-14T11:19:18.851515Z", + "iopub.status.busy": "2025-08-14T11:19:18.850941Z", + "iopub.status.idle": "2025-08-14T11:19:18.941694Z", + "shell.execute_reply": "2025-08-14T11:19:18.941103Z", + "shell.execute_reply.started": "2025-08-14T11:19:18.851460Z" + } + }, + "outputs": [], + "source": [ + "import json\n", + "import csv\n", + "import openpyxl\n", + "\n", + "filename = 'data/南京化工体检情况.json'\n", + "with open(filename,'r') as fl:\n", + " dict1 = json.load(fl)\n", + "\n", + "list1 = [\"总胆固醇\",\"甘油三酯\",\"空腹血糖\",\"糖化血红蛋白\",\"谷丙转氨酶 (ALT)\",\"谷草转氨酶 (AST)\",\"γ- 谷氨酰转肽酶 (GGT)\",\"促甲状腺激素 (TSH)\",\"游离三碘甲状腺原氨酸 (FT3)\",\"游离甲状腺素 (FT4)\",\"肌酐\",\"尿素氮\",\"尿酸\",\"尿微量白蛋白\"]\n", + "title = ['编号','姓名','性别','血压','状态']\n", + "for item in list1:\n", + " title.append(item)\n", + " title.append('状态')\n", + "list3 = []\n", + "for k, v in dict1.items():\n", + " list2 = []\n", + " list2.append(k)\n", + " list2.append(v['name'])\n", + " list2.append(v['sex'])\n", + " if '血压' in v.keys():\n", + " xueya = v['血压']['舒张压']+'/'+v['血压']['收缩压']\n", + " if '状态' in v['血压'].keys():\n", + " zt = v['血压']['状态']\n", + " else:\n", + " zt = ''\n", + " else:\n", + " xueya = ''\n", + " zt = ''\n", + " \n", + " list2.append(xueya)\n", + " list2.append(zt)\n", + " for item in list1:\n", + " if item in v.keys() and '结果' in v[item]:\n", + " list2.append(v[item]['结果'])\n", + " if '状态' in v[item]:\n", + " list2.append(v[item]['状态'])\n", + " else:\n", + " list2.append('')\n", + " else:\n", + " list2.append('')\n", + " list2.append('') \n", + " list3.append(list2)\n", + "\n", + "filename = 'data/南京化工体检相关数据明细.xlsx'\n", + "wb = openpyxl.Workbook()\n", + "sheet = wb.active\n", + "sheet.append(title)\n", + "for row in list3:\n", + " sheet.append(row)\n", + " \n", + "wb.save(filename)" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "87556725-f68a-484b-827c-f6735155e940", + "id": "221b35dc-3a17-4950-993c-b634ee9a53cf", "metadata": {}, "outputs": [], "source": []