This commit is contained in:
512song committed 2025-11-26 15:29:09 +08:00
1 parent aff0303bc9
commit d41474d9f1
6 files changed
+662 -1607

No files matched your search

+184 -119
View File
@@ -1,143 +1,208 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"import pymysql\n",
"db = pymysql.connect(\"localhost\",\"songyi\",\"yylzs\",\"mydata\" )\n",
"cursor = db.cursor()\n",
"cursor.execute(\"SELECT VERSION()\")\n",
"data = cursor.fetchone()\n",
"print (\"Database version : %s \" % data)\n",
"db.close()"
"# Django数据处理"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 初始化数据库"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pymysql\n",
"db = pymysql.connect(\"localhost\",\"songyi\",\"yylzs\",\"mydata\" )\n",
"cursor = db.cursor()\n",
"sql = \"select id,name from sports_person where name= %s\"\n",
"cursor.execute(sql, ('张联红',))\n",
"result = cursor.fetchone()\n",
"print(result)\n",
"db.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pymysql\n",
"db = pymysql.connect(\"localhost\",\"songyi\",\"yylzs\",\"mydata\" )\n",
"cursor = db.cursor()\n",
"sql = \"select id,name from sports_person\"\n",
"cursor.execute(sql)\n",
"result = cursor.fetchone()\n",
"print(\"人员信息:\")\n",
"print(result)\n",
"sql = \"select id,name from sports_item\"\n",
"cursor.execute(sql)\n",
"result = cursor.fetchone()\n",
"print(\"\\n运动项目信息:\")\n",
"print(result)\n",
"db.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pymysql\n",
"db = pymysql.connect(\"localhost\",\"songyi\",\"yylzs\",\"mydata\" )\n",
"cursor = db.cursor()\n",
"sql = \"select a.target_id,b.name from sports_item_target as a,sports_target as b where a.target_id=b.id\"\n",
"cursor.execute(sql)\n",
"re_ta = {}\n",
"print(\"\\n运动项目信息:\")\n",
"results = cursor.fetchall()\n",
"for result in results:\n",
"# print (\"%d--%s\" %(result[0],result[1]))\n",
" re_ta[result[0]] = result[1]\n",
" print(\"re_ta[%d]= #%s\" %(result[0],result[1]))\n",
"print(re_ta)\n",
"db.close()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pymysql\n",
"db = pymysql.connect(\"localhost\",\"songyi\",\"yylzs\",\"mydata\" )\n",
"cursor = db.cursor()\n",
"person_id = \n",
"record_date = ''\n",
"item_id =\n",
"re_ta = {}\n",
"re_ta[1]= #运动距离\n",
"re_ta[2]= #运动时间\n",
"re_ta[3]= #平均配速\n",
"re_ta[4]= #平均速度\n",
"re_ta[5]= #平均步频\n",
"re_ta[6]= #平均步幅\n",
"re_ta[7]= #步数\n",
"re_ta[8]= #平均心率\n",
"re_ta[9]= #最大心率\n",
"re_ta[10]= #无氧耐力\n",
"re_ta[11]= #有氧耐力\n",
"re_ta[12]= #最大步频\n",
"re_ta[13]= #最快配速\n",
"sql = \"select * from sports_person\"\n",
"db.close()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 44,
"metadata": {
"execution": {
"iopub.execute_input": "2023-03-27T06:10:30.861854Z",
"iopub.status.busy": "2023-03-27T06:10:30.861008Z",
"iopub.status.idle": "2023-03-27T06:10:30.877436Z",
"shell.execute_reply": "2023-03-27T06:10:30.876689Z",
"shell.execute_reply.started": "2023-03-27T06:10:30.861813Z"
},
"tags": []
"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": [
"100\n"
"(1, '宁夏能化员工干预', 1, '中石化宁夏能化公司')\n",
"(2, '天津石化2025年体质检测', 2, '中石化(天津)石油化工有限公司')\n",
"(3, '新疆采油工艺研究院2025年11月体质检测', 3, '新疆油田采油工艺研究院')\n"
]
}
],
"source": [
"import json\n",
"import pymongo\n",
"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",
"myclient = pymongo.MongoClient('mongodb://localhost:27017/')\n",
"mydb = myclient[\"tice\"]\n",
"mycol = mydb[\"test\"]\n",
"renyuan = set()\n",
"for x in mycol.find():\n",
" renyuan.add(x['name'])\n",
"print(len(renyuan))\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",
" "
]
},
{
@@ -164,7 +229,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.12.3"
}
},
"nbformat": 4,