20251119
This commit is contained in:
1 parent
31ddcb887f
commit
4f0f65a076
6 files changed
+1896
-136
No files matched your search
+210
-23
@@ -1366,10 +1366,10 @@
|
||||
" dict2 = {}\n",
|
||||
" code = item['data']['code']\n",
|
||||
" rq = item['date_created'].split()[0]\n",
|
||||
" dict2['height'] = item['data']['height']\n",
|
||||
" dict2['weight'] = item['data']['weight']\n",
|
||||
" dict2['next_weight'] = item['data']['weight']-item['data']['next_weight']\n",
|
||||
" dict2['bmi'] = (item['data']['weight']/item['data']['height'])/item['data']['height']*10000\n",
|
||||
" dict2['height'] = str('%.2f' % item['data']['height'])\n",
|
||||
" dict2['weight'] = str('%.2f' %item['data']['weight'])\n",
|
||||
" dict2['next_weight'] = str('%.2f' %(item['data']['weight']-item['data']['next_weight']))\n",
|
||||
" dict2['bmi'] = str('%.2f' %((item['data']['weight']/item['data']['height'])/item['data']['height']*10000))\n",
|
||||
" bmi = (item['data']['weight']/item['data']['height'])/item['data']['height']*10000\n",
|
||||
" if bmi<18.5:\n",
|
||||
" bmi_zt = '体重偏轻'\n",
|
||||
@@ -1380,25 +1380,25 @@
|
||||
" else:\n",
|
||||
" bmi_zt = '体重肥胖'\n",
|
||||
" dict2['bmi_zt'] = bmi_zt\n",
|
||||
" dict2['waist'] = item['data']['waist']\n",
|
||||
" dict2['hip'] = item['data']['hip']\n",
|
||||
" whr = dict2['waist']/dict2['hip']\n",
|
||||
" dict2['waist'] = str('%.2f' %item['data']['waist'])\n",
|
||||
" dict2['hip'] = str('%.2f' %item['data']['hip'])\n",
|
||||
" whr = item['data']['waist']/item['data']['hip']\n",
|
||||
" if item['data']['gender'] =='male' and whr > 0.9:\n",
|
||||
" whr_zt = '超标'\n",
|
||||
" elif item['data']['gender'] =='female' and whr > 0.85:\n",
|
||||
" whr_zt = '超标'\n",
|
||||
" else:\n",
|
||||
" whr_zt = '正常'\n",
|
||||
" dict2['whr'] = whr\n",
|
||||
" dict2['whr'] = str('%.2f' %whr)\n",
|
||||
" dict2['whr_zt'] = whr_zt\n",
|
||||
" if 'sport_type' in item['data'].keys(): \n",
|
||||
" dict2['sport_type'] = ','.join(item['data']['sport_type']).replace('other','其他')\n",
|
||||
" dict2['sport_duration'] = item['data']['sport_duration']\n",
|
||||
" dict2['sport_duration'] = str(item['data']['sport_duration'])\n",
|
||||
" dict2['recipe'] = item['data']['recipe']\n",
|
||||
" if 'last_sport' in item['data'].keys(): \n",
|
||||
" dict2['last_sport'] = item['data']['last_sport']\n",
|
||||
" if 'last_sport_time' in item['data'].keys():\n",
|
||||
" dict2['last_sport_time'] = item['data']['last_sport_time']\n",
|
||||
" dict2['last_sport_time'] = str(item['data']['last_sport_time'])\n",
|
||||
" if 'last_sport-Comment' in item['data'].keys():\n",
|
||||
" dict2['last_sport-Comment'] = item['data']['last_sport-Comment']\n",
|
||||
" dict2['last_recipe'] = item['data']['last_recipe']\n",
|
||||
@@ -1407,12 +1407,12 @@
|
||||
" if 'last_lose' in item['data'].keys(): \n",
|
||||
" dict2['last_lose'] = item['data']['last_lose']\n",
|
||||
" if 'last_lose_weight' in item['data'].keys():\n",
|
||||
" dict2['last_lose_weight'] = item['data']['last_lose_weight']\n",
|
||||
" dict2['last_lose_weight'] = str('%.2f' %item['data']['last_lose_weight'])\n",
|
||||
" if 'last_lose-Comment' in item['data'].keys(): \n",
|
||||
" dict2['last_lose-Comment'] = item['data']['last_lose-Comment']\n",
|
||||
" if code in person.keys():\n",
|
||||
" person[code][rq] = dict2\n",
|
||||
"filename = 'data/宁夏能化干预人员问卷情况.json'\n",
|
||||
"filename = 'data/宁夏能化干预人员问卷情况-1.json'\n",
|
||||
"\n",
|
||||
"with open(filename,'w') as fl:\n",
|
||||
" json.dump(person, fl, ensure_ascii=False)\n",
|
||||
@@ -1430,17 +1430,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"execution_count": null,
|
||||
"id": "633c35d3-61f0-4a9d-9982-441e4c1291b9",
|
||||
"metadata": {
|
||||
"execution": {
|
||||
"iopub.execute_input": "2025-11-17T09:35:16.409939Z",
|
||||
"iopub.status.busy": "2025-11-17T09:35:16.409246Z",
|
||||
"iopub.status.idle": "2025-11-17T09:35:16.664860Z",
|
||||
"shell.execute_reply": "2025-11-17T09:35:16.664302Z",
|
||||
"shell.execute_reply.started": "2025-11-17T09:35:16.409918Z"
|
||||
}
|
||||
},
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
@@ -1514,10 +1506,205 @@
|
||||
"wb.save(filename) "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "0fe62c71-eb58-4204-85bc-dc5497231df4",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 问卷信息汇总导入数据库"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 77,
|
||||
"id": "55b20059-ae36-493e-b0c5-c7e35473efc7",
|
||||
"metadata": {
|
||||
"execution": {
|
||||
"iopub.execute_input": "2025-11-19T10:42:23.125996Z",
|
||||
"iopub.status.busy": "2025-11-19T10:42:23.125745Z",
|
||||
"iopub.status.idle": "2025-11-19T10:42:25.142922Z",
|
||||
"shell.execute_reply": "2025-11-19T10:42:25.142275Z",
|
||||
"shell.execute_reply.started": "2025-11-19T10:42:23.125974Z"
|
||||
}
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"import openpyxl\n",
|
||||
"import psycopg2\n",
|
||||
"from datetime import datetime\n",
|
||||
"\n",
|
||||
"conn = psycopg2.connect(\n",
|
||||
" host=\"localhost\",\n",
|
||||
" database=\"tice\",\n",
|
||||
" user=\"mydata\",\n",
|
||||
" password=\"songyi\"\n",
|
||||
")\n",
|
||||
"cur = conn.cursor()\n",
|
||||
"event_id = 1\n",
|
||||
"org_id = 1\n",
|
||||
"filename = 'data/宁夏能化干预人员问卷情况-1.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",
|
||||
" code = k\n",
|
||||
" \n",
|
||||
" sql = \"SELECT id from fitness_person where code = %s and org_id = %s\"\n",
|
||||
" params = (code,org_id)\n",
|
||||
" cur.execute(sql, params)\n",
|
||||
" rows = cur.fetchone()\n",
|
||||
" if rows:\n",
|
||||
" data1 = []\n",
|
||||
" data1.append(event_id)\n",
|
||||
" data1.append(rows[0])\n",
|
||||
" data1.append(rq)\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",
|
||||
" \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(tuple(data1))\n",
|
||||
" \n",
|
||||
"sql = \"INSERT INTO fitness_record_data (event_id,user_id,date_created, height, weight, next_weight, bmi,bmi_zt,waist, hip, whr,whr_zt, sport_type, sport_duration, recipe, last_sport, last_recipe, last_lose_comment)\" \n",
|
||||
"sql = sql + \" VALUES (%s,%s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s)\"\n",
|
||||
"cur.executemany(sql, data2)\n",
|
||||
"conn.commit()\n",
|
||||
"\n",
|
||||
"# 关闭连接\n",
|
||||
"cur.close()\n",
|
||||
"conn.close()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c19f04a8-64a4-4660-8359-7780de4a5227",
|
||||
"id": "a068cd21-d751-46f9-9c95-f26cf14ddeb8",
|
||||
"metadata": {},
|
||||
"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()\n",
|
||||
"event_id = 1\n",
|
||||
"org_id = 1\n",
|
||||
"filename = 'data/宁夏能化干预人员问卷情况-1.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",
|
||||
" code = k\n",
|
||||
" \n",
|
||||
" sql = \"SELECT id from fitness_person where code = %s and org_id = %s\"\n",
|
||||
" params = (code,org_id)\n",
|
||||
" cur.execute(sql, params)\n",
|
||||
" rows = cur.fetchone()\n",
|
||||
" if rows:\n",
|
||||
" data1 = []\n",
|
||||
" data1.append(event_id)\n",
|
||||
" data1.append(rows[0])\n",
|
||||
" data1.append(datetime.strptime(rq,'%Y-%m-%d'))\n",
|
||||
" data1.append(k)\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",
|
||||
" print(data1)\n",
|
||||
" sql = \"INSERT INTO fitness_record_data (event_id,user_id,date_created, height, weight, next_weight, bmi,bmi_zt,waist, hip, whr,whr_zt, sport_type, sport_duration, recipe, last_sport, last_recipe, last_lose, last_lose_comment)\" \n",
|
||||
"sql = sql + \" VALUES (%s,%s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s, %s,%s, %s, %s, %s, %s)\"\n",
|
||||
"cur.executemany(sql, data2)\n",
|
||||
"conn.commit()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "c4cbfed3-51cf-47b8-af20-eee4ac61b556",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
|
||||
Reference in new issue
Block a user