{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Django数据处理" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 初始化数据库" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "execution": { "iopub.execute_input": "2025-11-26T04:58:22.889919Z", "iopub.status.busy": "2025-11-26T04:58:22.889099Z", "iopub.status.idle": "2025-11-26T04:58:22.916971Z", "shell.execute_reply": "2025-11-26T04:58:22.916139Z", "shell.execute_reply.started": "2025-11-26T04:58:22.889849Z" } }, "outputs": [], "source": [ "import json\n", "import openpyxl\n", "import psycopg2\n", "\n", "conn = psycopg2.connect(\n", " host=\"localhost\",\n", " database=\"tice\",\n", " user=\"mydata\",\n", " password=\"songyi\"\n", ")\n", "cur = conn.cursor()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 列示单位及事件" ] }, { "cell_type": "code", "execution_count": 29, "metadata": { "execution": { "iopub.execute_input": "2025-11-26T03:18:44.997174Z", "iopub.status.busy": "2025-11-26T03:18:44.996492Z", "iopub.status.idle": "2025-11-26T03:18:45.006209Z", "shell.execute_reply": "2025-11-26T03:18:45.005343Z", "shell.execute_reply.started": "2025-11-26T03:18:44.997112Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, '宁夏能化员工干预', 1, '中石化宁夏能化公司')\n", "(2, '天津石化2025年体质检测', 2, '中石化(天津)石油化工有限公司')\n", "(3, '新疆采油工艺研究院2025年11月体质检测', 3, '新疆油田采油工艺研究院')\n" ] } ], "source": [ "sql = \"SELECT e.id,e.name,e.org_id,o.name from fitness_event as e,fitness_org as o where e.org_id = o.id\"\n", "\n", "cur.execute(sql)\n", "rows = cur.fetchall()\n", "for row in rows:\n", " print(row)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 导入事件人员信息" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "execution": { "iopub.execute_input": "2025-11-26T02:26:51.443764Z", "iopub.status.busy": "2025-11-26T02:26:51.443185Z", "iopub.status.idle": "2025-11-26T02:26:51.471921Z", "shell.execute_reply": "2025-11-26T02:26:51.470918Z", "shell.execute_reply.started": "2025-11-26T02:26:51.443710Z" } }, "outputs": [], "source": [ "event_id = 3\n", "sql = \"SELECT code from fitness_person where org_id = %s\"\n", "params = (event_id,)\n", "cur.execute(sql, params)\n", "rows = cur.fetchall()\n", "codes = []\n", "for row in rows:\n", " codes.append(row[0])\n", "filename = '体测单位/data/新疆油田采油工艺研究院-202511.json'\n", "with open(filename,'r') as fl:\n", " dict1 = json.load(fl)\n", "data1 = []\n", "for k, v in dict1.items():\n", " if k not in codes:\n", " if 'sub_unit' in v.keys():\n", " sub_unit = v['sub_unit']\n", " else:\n", " sub_unit = ''\n", " if 'phone' in v.keys():\n", " phone = v['phone']\n", " else:\n", " phone = ''\n", " tuple1 = (k,v['name'],v['sex'],v['unit'],sub_unit,v['birth'],phone,event_id)\n", " data1.append(tuple1)\n", " \n", "sql = \"INSERT INTO exam_person ( code,name, sex,unit,sub_unit,birth,phone,event_id) VALUES (%s, %s, %s,%s, %s, %s, %s, %s)\" \n", "#print(data1)\n", "cur.executemany(sql, data1)\n", "conn.commit()\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 导入测试成绩" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "execution": { "iopub.execute_input": "2025-11-26T05:16:17.188547Z", "iopub.status.busy": "2025-11-26T05:16:17.187969Z", "iopub.status.idle": "2025-11-26T05:16:17.273706Z", "shell.execute_reply": "2025-11-26T05:16:17.272752Z", "shell.execute_reply.started": "2025-11-26T05:16:17.188494Z" } }, "outputs": [], "source": [ "import json\n", "import csv\n", "\n", "event_id = 3\n", "sql = \"SELECT code,sex,id from exam_person where event_id = %s\"\n", "params = (event_id,)\n", "cur.execute(sql, params)\n", "rows = cur.fetchall()\n", "dict1 = {}\n", "for row in rows:\n", " dict1.setdefault(row[0],{})\n", " dict1[row[0]]['id']= row[2]\n", " dict1[row[0]]['sex']= row[1]\n", "list1 = []\n", "filename = '体测单位/data/marks_20251126.csv'\n", "with open(filename,'r',newline='') as csv_file:\n", " fl = csv.reader(csv_file,delimiter=',')\n", " header = next(fl) \n", " for line in fl:\n", " #line = re.sub('[\\r\\n\\f ]{1,}', '', line)\n", " list1.append(line)\n", "list2 = []\n", "for result in list1:\n", " user = str(result[2]).lower() \n", " if user in dict1.keys(): \n", " #rq = date.fromisoformat(result[5].replace('/','-'))\n", " m_item = str(result[3])\n", " if dict1[user]['sex'] == '男':\n", " l_xm = ['bmi','lung','grip','flexion','jump','pushup','balance','reaction','step']\n", " else:\n", " l_xm = ['bmi','lung','grip','flexion','jump','balance','reaction','step','situp']\n", " item_name = result[3] \n", " if item_name in l_xm:\n", " rq = result[5].replace('/','-').split(' ')[0]\n", " if item_name =='bmi':\n", " height = result[4].split(',')[0]\n", " weight = result[4].split(',')[1]\n", " tuple1 = (event_id,dict1[user]['id'],user,rq,'height',height,result[6])\n", " list2.append(tuple1)\n", " tuple1 = (event_id,dict1[user]['id'],user,rq,'weight',weight,result[6])\n", " list2.append(tuple1) \n", " else:\n", " tuple1 = (event_id,dict1[user]['id'],user,rq,result[3],result[4],result[6])\n", " list2.append(tuple1)\n", "sql = \"INSERT INTO exam_record_data (event_id,user_id,code,date_exam,item,mark,date_created) VALUES (%s,%s, %s, %s,%s, %s, %s)\" \n", "#print(data1)\n", "cur.executemany(sql, list2)\n", "conn.commit()\n", " " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 4 }