Files
jupyter/数据库操作.ipynb
T
2025-11-26 15:29:09 +08:00

7.1 KiB

Django数据处理

初始化数据库

In [44]:
import json
import openpyxl
import psycopg2

conn = psycopg2.connect(
    host="localhost",
    database="tice",
    user="mydata",
    password="songyi"
)
cur = conn.cursor()

列示单位及事件

In [29]:
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"

cur.execute(sql)
rows = cur.fetchall()
for row in rows:
    print(row)
(1, '宁夏能化员工干预', 1, '中石化宁夏能化公司')
(2, '天津石化2025年体质检测', 2, '中石化(天津)石油化工有限公司')
(3, '新疆采油工艺研究院2025年11月体质检测', 3, '新疆油田采油工艺研究院')

导入事件人员信息

In [17]:
event_id = 3
sql = "SELECT code from fitness_person where org_id = %s"
params = (event_id,)
cur.execute(sql, params)
rows = cur.fetchall()
codes = []
for row in rows:
    codes.append(row[0])
filename = '体测单位/data/新疆油田采油工艺研究院-202511.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
data1 = []
for k, v in dict1.items():
    if k not in codes:
        if 'sub_unit' in v.keys():
            sub_unit = v['sub_unit']
        else:
            sub_unit = ''
        if 'phone' in v.keys():
            phone = v['phone']
        else:
            phone = ''
        tuple1 = (k,v['name'],v['sex'],v['unit'],sub_unit,v['birth'],phone,event_id)
        data1.append(tuple1)
        
sql = "INSERT INTO exam_person ( code,name, sex,unit,sub_unit,birth,phone,event_id) VALUES (%s, %s, %s,%s, %s, %s, %s, %s)"  
#print(data1)
cur.executemany(sql, data1)
conn.commit()
        

导入测试成绩

In [46]:
import json
import csv

event_id = 3
sql = "SELECT code,sex,id from exam_person where event_id = %s"
params = (event_id,)
cur.execute(sql, params)
rows = cur.fetchall()
dict1 = {}
for row in rows:
    dict1.setdefault(row[0],{})
    dict1[row[0]]['id']= row[2]
    dict1[row[0]]['sex']= row[1]
list1 = []
filename = '体测单位/data/marks_20251126.csv'
with open(filename,'r',newline='') as csv_file:
    fl = csv.reader(csv_file,delimiter=',')
    header = next(fl)    
    for line in fl:
        #line = re.sub('[\r\n\f ]{1,}', '', line)
        list1.append(line)
list2 = []
for result in list1:
    user = str(result[2]).lower() 
    if user in dict1.keys():        
        #rq = date.fromisoformat(result[5].replace('/','-'))
        m_item = str(result[3])
        if dict1[user]['sex'] == '男':
            l_xm = ['bmi','lung','grip','flexion','jump','pushup','balance','reaction','step']
        else:
            l_xm = ['bmi','lung','grip','flexion','jump','balance','reaction','step','situp']
        item_name = result[3]     
        if item_name in l_xm:
            rq = result[5].replace('/','-').split(' ')[0]
            if item_name =='bmi':
                height = result[4].split(',')[0]
                weight = result[4].split(',')[1]
                tuple1 = (event_id,dict1[user]['id'],user,rq,'height',height,result[6])
                list2.append(tuple1)
                tuple1 = (event_id,dict1[user]['id'],user,rq,'weight',weight,result[6])
                list2.append(tuple1) 
            else:
                tuple1 = (event_id,dict1[user]['id'],user,rq,result[3],result[4],result[6])
                list2.append(tuple1)
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)"  
#print(data1)
cur.executemany(sql, list2)
conn.commit()
        
In [ ]: