简化宁夏能化数据导入流程
This commit is contained in:
512song committed 2025-12-07 23:55:37 +08:00
1 parent 3c756eda13
commit c19ca272d7
1 file changed
+19 -221
+19 -221
View File
@@ -1131,17 +1131,9 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"id": "22be8f39-afbb-448a-8ae6-c825c7d38757", "id": "22be8f39-afbb-448a-8ae6-c825c7d38757",
"metadata": { "metadata": {},
"execution": {
"iopub.execute_input": "2025-12-01T08:20:03.713528Z",
"iopub.status.busy": "2025-12-01T08:20:03.713011Z",
"iopub.status.idle": "2025-12-01T08:20:04.212086Z",
"shell.execute_reply": "2025-12-01T08:20:04.211619Z",
"shell.execute_reply.started": "2025-12-01T08:20:03.713468Z"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"import json\n", "import json\n",
@@ -1150,7 +1142,7 @@
"\n", "\n",
"title = []\n", "title = []\n",
"\n", "\n",
"filename = 'data/surveys_records_2025-11-30.json'\n", "filename = 'data/surveys_records_2025-12-07.json'\n",
"with open(filename,'r') as fl:\n", "with open(filename,'r') as fl:\n",
" dict1 = json.load(fl)\n", " dict1 = json.load(fl)\n",
"list1 = []\n", "list1 = []\n",
@@ -1175,7 +1167,7 @@
" \n", " \n",
" list1.append(list2)\n", " list1.append(list2)\n",
"all_title = ['id', 'date_created','name', 'gender', 'birth', 'code', 'unit', 'height', 'weight', 'next_weight', 'waist', 'hip', 'level4', 'level3', 'level2', 'recipe', 'level1', 'last_level4', 'last_level3', 'last_level2', 'last_level1', 'last_recipe', 'last_lose', 'last_sport', 'sport_type', 'sport_duration', 'last_sport_time', 'last_lose-Comment', 'last_recipe-Comment', 'last_lose_weight', 'last_sport-Comment', 'sport_type-Comment']\n", "all_title = ['id', 'date_created','name', 'gender', 'birth', 'code', 'unit', 'height', 'weight', 'next_weight', 'waist', 'hip', 'level4', 'level3', 'level2', 'recipe', 'level1', 'last_level4', 'last_level3', 'last_level2', 'last_level1', 'last_recipe', 'last_lose', 'last_sport', 'sport_type', 'sport_duration', 'last_sport_time', 'last_lose-Comment', 'last_recipe-Comment', 'last_lose_weight', 'last_sport-Comment', 'sport_type-Comment']\n",
"filename = 'data/宁夏能化干预人员问卷(20251130).xlsx'\n", "filename = 'data/宁夏能化干预人员问卷(20251207).xlsx'\n",
"wb = openpyxl.Workbook()\n", "wb = openpyxl.Workbook()\n",
"sheet = wb.active\n", "sheet = wb.active\n",
"sheet.append(all_title)\n", "sheet.append(all_title)\n",
@@ -1203,16 +1195,16 @@
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "88f6e148-bfd3-4394-8cfd-de7c84306431", "id": "d266379b-4797-4f60-a83d-dc2349a48f0b",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### 导入人员信息" "### 核对新增人员"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
"id": "747ff8ae-9e50-452f-9dcf-f5ba7c5b8f51", "id": "91c53866-3725-4ceb-b0eb-23fb14e20561",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@@ -1227,92 +1219,7 @@
" password=\"songyi\"\n", " password=\"songyi\"\n",
")\n", ")\n",
"cur = conn.cursor()\n", "cur = conn.cursor()\n",
"wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251130).xlsx',data_only=True)\n", "wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251207).xlsx',data_only=True)\n",
"sheet = wb.active\n",
"# sheets = wb.sheetnames\n",
"person = {}\n",
"for n in range(2, sheet.max_row+1):\n",
" code = str(sheet.cell(n, 6).value)\n",
" person.setdefault(code, {})\n",
" dict1 = {}\n",
" dict1['name'] = sheet.cell(n, 3).value\n",
" dict1['sex'] = sheet.cell(n, 4).value\n",
" birth = str(sheet.cell(n, 5).value).split()[0]\n",
" if len(birth)==7:\n",
" dict1['birth'] = birth+'-01'\n",
" else:\n",
" dict1['birth'] = birth\n",
" dict1['unit'] = sheet.cell(n, 7).value\n",
" person[code] = dict1\n",
"org_id = 1\n",
"data1 = []\n",
"for k,v in person.items():\n",
" tuple1 = (v['name'],v['sex'],v['unit'],v['birth'],k,org_id)\n",
" print(tuple1)\n",
" data1.append(tuple1)\n",
"sql = \"INSERT INTO fitness_person ( name, sex,unit,birth,code,org_id) VALUES (%s, %s, %s,%s, %s, %s)\" \n",
"#print(data1)\n",
"cur.executemany(sql, data1)\n",
"conn.commit()\n",
"\n",
"# 关闭连接\n",
"cur.close()\n",
"conn.close()"
]
},
{
"cell_type": "markdown",
"id": "d266379b-4797-4f60-a83d-dc2349a48f0b",
"metadata": {},
"source": [
"### 核对新增人员"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "91c53866-3725-4ceb-b0eb-23fb14e20561",
"metadata": {
"execution": {
"iopub.execute_input": "2025-12-01T08:22:23.093768Z",
"iopub.status.busy": "2025-12-01T08:22:23.093084Z",
"iopub.status.idle": "2025-12-01T08:22:23.315461Z",
"shell.execute_reply": "2025-12-01T08:22:23.314813Z",
"shell.execute_reply.started": "2025-12-01T08:22:23.093711Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('李涛', '男', '企业管理部(法律事务部)', '1984-10-16', '02020309', 1)\n",
"('马宏伟', '男', 'BDO运行部', '1998-08-26', '03497946', 1)\n",
"('吴婷', '女', '电气仪表中心', '1989-06-12', '02020010', 1)\n",
"('罗继发', '男', '安全环保部', '1986-09-15', '02019647', 1)\n",
"('苟小锐', '男', '安全环保部', '1974-04-08', '02018416', 1)\n",
"('王振华', '男', '设备工程部', '1981-10-31', '02018482', 1)\n",
"('莫文宁', '男', '物资采购中心', '1972-09-02', '02020208', 1)\n",
"('张保华', '男', '质检中心', '1991-12-11', '02018721', 1)\n",
"('邓蓉芳', '女', '质检中心', '1980-11-12', '02018677', 1)\n",
"('冯波', '男', '乙炔运行部', '1984-07-01', '02019339', 1)\n",
"('尹小俊', '男', '乙炔运行部', '1977-06-05', '2019493', 1)\n"
]
}
],
"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()\n",
"wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251130).xlsx',data_only=True)\n",
"sheet = wb.active\n", "sheet = wb.active\n",
"# sheets = wb.sheetnames\n", "# sheets = wb.sheetnames\n",
"org_id = 1\n", "org_id = 1\n",
@@ -1350,6 +1257,7 @@
" tuple1 = (v['name'],v['sex'],v['unit'],v['birth'],k,org_id)\n", " tuple1 = (v['name'],v['sex'],v['unit'],v['birth'],k,org_id)\n",
" print(tuple1)\n", " print(tuple1)\n",
" data1.append(tuple1)\n", " data1.append(tuple1)\n",
"print(len(data1))\n",
"sql = \"INSERT INTO fitness_person ( name, sex,unit,birth,code,org_id) VALUES (%s, %s, %s,%s, %s, %s)\" \n", "sql = \"INSERT INTO fitness_person ( name, sex,unit,birth,code,org_id) VALUES (%s, %s, %s,%s, %s, %s)\" \n",
"#print(data1)\n", "#print(data1)\n",
"cur.executemany(sql, data1)\n", "cur.executemany(sql, data1)\n",
@@ -1365,7 +1273,7 @@
"id": "5c2a37a0-18d3-4c89-8dd9-283a417b5c60", "id": "5c2a37a0-18d3-4c89-8dd9-283a417b5c60",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### 导入问卷信息" "### 导入问卷信息(已作废)"
] ]
}, },
{ {
@@ -1438,31 +1346,15 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": null,
"id": "b7133988-213c-4b52-8d6b-17b56b8ea310", "id": "b7133988-213c-4b52-8d6b-17b56b8ea310",
"metadata": { "metadata": {},
"execution": { "outputs": [],
"iopub.execute_input": "2025-12-01T08:24:08.522567Z",
"iopub.status.busy": "2025-12-01T08:24:08.522021Z",
"iopub.status.idle": "2025-12-01T08:24:08.789832Z",
"shell.execute_reply": "2025-12-01T08:24:08.789290Z",
"shell.execute_reply.started": "2025-12-01T08:24:08.522517Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"361 ok\n"
]
}
],
"source": [ "source": [
"import json\n", "import json\n",
"import openpyxl\n", "import openpyxl\n",
"\n", "\n",
"wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251130).xlsx',data_only=True)\n", "wb = openpyxl.load_workbook('data/宁夏能化干预人员问卷(20251207).xlsx',data_only=True)\n",
"sheet = wb.active\n", "sheet = wb.active\n",
"# sheets = wb.sheetnames\n", "# sheets = wb.sheetnames\n",
"person = {}\n", "person = {}\n",
@@ -1481,7 +1373,7 @@
" person[code] = dict1\n", " person[code] = dict1\n",
"#print(person)\n", "#print(person)\n",
"\n", "\n",
"filename = 'data/surveys_records_2025-11-30.json'\n", "filename = 'data/surveys_records_2025-12-07.json'\n",
"with open(filename,'r') as fl:\n", "with open(filename,'r') as fl:\n",
" dict1 = json.load(fl)\n", " dict1 = json.load(fl)\n",
"list1 = []\n", "list1 = []\n",
@@ -1539,7 +1431,7 @@
" dict2['last_lose-Comment'] = data['last_lose-Comment']\n", " dict2['last_lose-Comment'] = data['last_lose-Comment']\n",
" if code in person.keys():\n", " if code in person.keys():\n",
" person[code][rq] = dict2\n", " person[code][rq] = dict2\n",
"filename = 'data/宁夏能化干预人员问卷情况(20251130).json'\n", "filename = 'data/宁夏能化干预人员问卷情况(20251207).json'\n",
"\n", "\n",
"with open(filename,'w') as fl:\n", "with open(filename,'w') as fl:\n",
" json.dump(person, fl, ensure_ascii=False)\n", " json.dump(person, fl, ensure_ascii=False)\n",
@@ -1547,92 +1439,6 @@
" " " "
] ]
}, },
{
"cell_type": "markdown",
"id": "7805ebad-fe9d-4bc2-a537-c665fc831332",
"metadata": {},
"source": [
"## 问卷信息汇总导出"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "633c35d3-61f0-4a9d-9982-441e4c1291b9",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import openpyxl\n",
"\n",
"filename = 'data/宁夏能化干预人员问卷情况.json'\n",
"with open(filename,'r') as fl:\n",
" dict1 = json.load(fl)\n",
"list1 = ['name','sex','birth','unit']\n",
"xm = ['height','weight','next_weight','bmi','bmi_zt','waist', 'hip','whr','whr_zt' ]\n",
"data2 = []\n",
"for k,v in dict1.items():\n",
" list2 = []\n",
" \n",
" for k1,v1 in v.items():\n",
" #list2 = []\n",
" if k1 not in list1:\n",
" list2.append(k1)\n",
" list2.sort()\n",
" \n",
" for rq in list2:\n",
" for k1, v1 in v[rq].items():\n",
" data1 = []\n",
" data1.append(rq)\n",
" data1.append(k)\n",
" data1.append(v['unit'])\n",
" data1.append(v['name'])\n",
" data1.append(v['sex'])\n",
" data1.append(v['sex'])\n",
" for item in xm:\n",
" data1.append(v[rq][item])\n",
" if 'sport_type' in v[rq].keys():\n",
" data1.append(v[rq]['sport_type'])\n",
" else:\n",
" data1.append('')\n",
" if 'sport_type' in v[rq].keys():\n",
" data1.append(v[rq]['sport_type'])\n",
" else:\n",
" data1.append('')\n",
" \n",
" if 'sport_duration' in v[rq].keys():\n",
" data1.append(v[rq]['sport_duration'])\n",
" else:\n",
" data1.append('')\n",
" if v[rq]['recipe']=='free':\n",
" data1.append('自行控制')\n",
" else:\n",
" data1.append('减脂餐')\n",
" if v[rq]['last_sport']=='是':\n",
" data1.append('运动时长:'+str(v[rq]['last_sport_time'])+'分钟')\n",
" else:\n",
" data1.append('未运动,'+v[rq]['last_sport-Comment'])\n",
" if v[rq]['last_recipe']=='是':\n",
" data1.append('是')\n",
" else:\n",
" data1.append('否。'+v[rq]['last_recipe-Comment'])\n",
" if v[rq]['last_lose']=='是':\n",
" data1.append('是,减重'+str(v[rq]['last_lose_weight'])+'公斤。')\n",
" elif 'last_lose-Comment' in v[rq].keys():\n",
" data1.append('否。'+v[rq]['last_lose-Comment'])\n",
" else:\n",
" data1.append('否。')\n",
" data2.append(data1)\n",
" \n",
"filename = 'data/宁夏能化干预人员问卷信息(20251114).xlsx'\n",
"wb = openpyxl.Workbook()\n",
"sheet = wb.active\n",
"for row in data2:\n",
" sheet.append(row)\n",
" \n",
"wb.save(filename) "
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"id": "0fe62c71-eb58-4204-85bc-dc5497231df4", "id": "0fe62c71-eb58-4204-85bc-dc5497231df4",
@@ -1643,17 +1449,9 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"id": "55b20059-ae36-493e-b0c5-c7e35473efc7", "id": "55b20059-ae36-493e-b0c5-c7e35473efc7",
"metadata": { "metadata": {},
"execution": {
"iopub.execute_input": "2025-12-01T08:24:33.067615Z",
"iopub.status.busy": "2025-12-01T08:24:33.067012Z",
"iopub.status.idle": "2025-12-01T08:24:34.348805Z",
"shell.execute_reply": "2025-12-01T08:24:34.348172Z",
"shell.execute_reply.started": "2025-12-01T08:24:33.067560Z"
}
},
"outputs": [], "outputs": [],
"source": [ "source": [
"import json\n", "import json\n",
@@ -1670,7 +1468,7 @@
"cur = conn.cursor()\n", "cur = conn.cursor()\n",
"event_id = 1\n", "event_id = 1\n",
"org_id = 1\n", "org_id = 1\n",
"filename = 'data/宁夏能化干预人员问卷情况(20251130).json'\n", "filename = 'data/宁夏能化干预人员问卷情况(20251207).json'\n",
"with open(filename,'r') as fl:\n", "with open(filename,'r') as fl:\n",
" dict1 = json.load(fl)\n", " dict1 = json.load(fl)\n",
"list1 = ['name','sex','birth','unit']\n", "list1 = ['name','sex','birth','unit']\n",