Files
jupyter/体测单位/东营羊庙.ipynb
T
2023-03-31 14:45:04 +00:00

28 KiB

调查报告线下数据管理

羊庙人员信息导入

In [ ]:
import json

filename = 'data/羊庙测试人员信息.json'

with open(filename,'r') as fl:
    dict1 = json.load(fl) 
dict2 = {}
for k,v in dict1.items():
    for item in v:
        bh = item['avatar_id']
        del item['avatar_id']
        dict2[bh] = item
filename = 'data/羊庙人员名单.json'
with open(filename, 'w') as fl:
    json.dump(dict2, fl, ensure_ascii=False,default=str)
print('ok')

获取参加体测人员编号

In [ ]:
import json
import time
import csv

filename = '../item.json'
item = {}
unit = {}
with open(filename,'r') as fl:
    dict1 = json.load(fl) 
for k,v in dict1.items():
    item[k] = v
re_ta = {}
dict1 = {}
list1 = []
#print("\n运动项目信息:")
filename = 'data/140.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 = int(result[2])
    if user not in list2:
        list2.append(user)
list2.sort()
print(len(list2))
print(list2)

根据风险筛选统计卡获取信息

In [ ]:
import openpyxl
import json
import time

wb = openpyxl.load_workbook('data/杨庙运动风险筛查统计表.xlsx')
sheet = wb.active
person = {}
for n in range(2, sheet.max_row+1):
    code = int(sheet.cell(n, 1).value)
    person.setdefault(code, {})
    dict1 = {}
    if sheet.cell(n, 8).value is not None:
        name = sheet.cell(n, 8).value
    else:
        name ='不详'
    dict1['name'] = name
    if sheet.cell(n, 9).value is not None:
        dict1['birth'] = str(sheet.cell(n, 9).value).split(' ')[0]
    if sheet.cell(n, 10).value is not None:
        dict1['phone'] = str(sheet.cell(n, 10).value)
    if sheet.cell(n,11).value is not None:
        dict1['id_num'] = sheet.cell(n,11).value
    person[code] = dict1
print(len(person))
filename = 'data/杨庙运动风险筛查统计表信息.json'
with open(filename, 'w') as fl:
    json.dump(person, fl, ensure_ascii=False,default=str)
print('ok')

根据恢复版获取信息

In [ ]:
import openpyxl
import json
import time

wb = openpyxl.load_workbook('data/羊庙恢复版.xlsx')
sheet = wb.active
person = {}
for n in range(2, sheet.max_row+1):
    if sheet.cell(n, 4).value is not None:        
        code = int(sheet.cell(n, 4).value)
        person.setdefault(code, {})
        dict1 = {}
        name = sheet.cell(n, 5).value
        dict1['name'] = name
        dict1['bh'] = n - 1
        dict1['sex'] = sheet.cell(n, 6).value
        if sheet.cell(n, 9).value is not None:
            dict1['phone'] = str(sheet.cell(n, 9).value)
        if sheet.cell(n,8).value is not None:
            dict1['id_num'] = sheet.cell(n,8).value
        if sheet.cell(n,3).value is not None:
            dict1['unit'] = sheet.cell(n,3).value
        person[code] = dict1
print(len(person))
filename = 'data/杨庙恢复版信息.json'
with open(filename, 'w') as fl:
    json.dump(person, fl, ensure_ascii=False,default=str)
print('ok')

风险筛选统计卡可用人员信息

In [ ]:
re_ta = {}
dict1 = {}
list1 = []
#print("\n运动项目信息:")
filename = 'data/140.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 = int(result[2])
    if user not in list2:
        list2.append(user)
list2.sort()
set1 = set()
filename = 'data/杨庙恢复版信息.json'
with open(filename,'r') as fl:
    dict_base = json.load(fl)
for k, v in dict_base.items():
    set1.add(int(k))
filename = 'data/杨庙运动风险筛查统计表信息.json'

with open(filename,'r') as fl:
    dict1 = json.load(fl)
list_fx = []

for k,v in dict1.items():
    
    if int(k) in list2 and k not in dict_base.keys() and v['name'] != '不详':        
        set1.add(int(k))
#print(len(list_fx))
#print(len(set1)) 
for item in list2:
    if item not in set1:
        print(item)

补充恢复版信息

In [ ]:
filename = 'data/杨庙恢复版信息.json'
with open(filename,'r') as fl:
    dict_base = json.load(fl)
for k, v in dict_base.items():
    set1.add(int(k))
filename = 'data/杨庙运动风险筛查统计表信息.json'

with open(filename,'r') as fl:
    dict1 = json.load(fl)
for k,v in dict_base.items():
    if k in dict1.keys() and 'phone' not in v.keys() and 'phone' in dict1[k].keys():
        #print(k,v['name'])
        dict_base[k]['phone'] =  dict1[k]['phone']

for k, v in dict1.items():
    if int(k) in list2 and k not in dict_base.keys():
        dict_base[k] = v
        
print(len(dict_base))
#print(dict_base)
filename = 'data/羊庙人员名单.json'
with open(filename,'r') as fl:
    dict2 = json.load(fl)
i = 1
for k, v in dict_base.items():
    if 'bh' not in v.keys():
        name = v['name']
        n = 0
        for k1,v1 in dict2.items():
            if name == v1['name']:
                bh = k1
                n = n+1
        if n ==1:
            dict_base[k]['bh'] = bh
        else:
            print(i,k,v['name'])
            i+=1
filename = 'data/杨庙补充汇总信息.json'
with open(filename, 'w') as fl:
    json.dump(dict_base, fl, ensure_ascii=False,default=str)
print('ok')

导出汇总信息

In [ ]:
filename = 'data/杨庙补充汇总信息.json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
list_item = ['name','bh','sex','phone','unit','id_num']
list_data = []

for k, v in dict1.items():
    list_mx = []
    list_mx.append(k)
    for item in list_item:
        if item in v.keys():
            list_mx.append(v[item])
        else:
            list_mx.append('')
    list_data.append(list_mx)
#print(list_data)     
filename = 'data/羊庙汇总情况表.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
#sheet.append(title)
for row in list_data:
    sheet.append(row)
    
wb.save(filename) 
print('ok!')

成年问卷导入

In [ ]:
import openpyxl
import json

filename = 'data/杨庙体测明确人员信息(20230328).json'
with open(filename,'r') as fl:
    dict2 = json.load(fl)
wb = openpyxl.load_workbook('data/羊庙问卷统计表(成年组).xlsx')
sheet = wb.active
# sheets = wb.sheetnames

dict1 = {}
no_xinli = [108,389,268,270]
sheet = wb.active
data1 =list(sheet.values)
list_bh = data1[0][1:]
del data1[0]
print(len(list_bh))
for i in range(1,len(list_bh)+1):
    list2 = []
    
    for item in data1:
        list2.append(item[i])
    dict1[list_bh[i-1]] = list2
for k, v in dict1.items():
    #if 0 in v:
    #    print(k)
    if str(k) not in dict2.keys():
        print(k,'不在清理名单中')

#print(dict1)

成年问卷导出

In [ ]:
import openpyxl
import json

filename = 'data/杨庙体测明确人员信息(20230328).json'
with open(filename,'r') as fl:
    dict3 = json.load(fl)
wb = openpyxl.load_workbook('data/羊庙问卷统计表(成年组).xlsx')
sheet = wb.active
# sheets = wb.sheetnames
dict1 = {}
list_bh = []
no_xinli = [108,389,268,270]
zy = [2,7,9,11,12,21,24]
sheet = wb.active
data1 =list(sheet.values)
list_bh = data1[0][1:]
del data1[0]


for i in range(1,len(list_bh)+1):
    list2 = []
    
    for item in data1:
        list2.append(item[i])
    dict1[list_bh[i-1]] = list2
#print(dict1)
s = ''
for k,v in dict1.items():
    if str(k) in dict3.keys(): 
        if dict3[str(k)]['sex'] ==1:
            sex = "m"
        else:
            sex = "f"
        dict2 = {}
        list_mx = []
        list_mx.append(f'"Gender":"{sex}"')
        list_mx.append(f'"Age":"M"')
        dict2['surveyId'] = "merge1"
        dict2['name'] = dict3[str(k)]['name'] 
        name = dict3[str(k)]['name'] 
        dict2['code'] = str(k)
        ss = ''
        
        if k not in no_xinli:
            for i in range(0,47):
                list_mx.append(f'"q{i+1}M":{v[i]}')        
        
        for i in range(1,61):
            if i not in zy:
                list_mx.append(f'"q{i}":{v[46+i]}')
        for i in range(1,27):
            list_mx.append(f'"qv{i}":{v[106+i]}')
        ss = ','.join(list_mx)
        ss ='{'+ss+'}'
        dict2['data'] = ss
        sj = '2023-03-28 20:02:00'
        s= s+f'("merge1","{name}","{str(k)}",\'{ss}\',"{sj}"),'
        #print(dict2)
print(s)

老年问卷导入

In [ ]:
import openpyxl
import json

filename = 'data/杨庙体测明确人员信息(20230328).json'
with open(filename,'r') as fl:
    dict2 = json.load(fl)
wb = openpyxl.load_workbook('data/羊庙问卷统计表(老年组).xlsx')
sheet = wb.active
# sheets = wb.sheetnames
dict1 = {}
dict3 = {}
no_xinli = [286]
sheet = wb.active
data1 =list(sheet.values)
list_bh = data1[0][1:]
print(list_bh)
del data1[0]
print(len(list_bh))
for i in range(1,len(list_bh)+1):
    list2 = []
    
    for item in data1:
        list2.append(item[i])
    dict1[list_bh[i-1]] = list2
for k, v in dict1.items():
    #if 0 in v or ' ' in v:
    #    print(k)
    if str(k) not in dict2.keys():
        print(k,'不在清理名单中')
#print(dict1)

老年问卷导出

In [ ]:
import openpyxl
import json

filename = 'data/杨庙体测明确人员信息(20230328).json'
with open(filename,'r') as fl:
    dict3 = json.load(fl)
wb = openpyxl.load_workbook('data/羊庙问卷统计表(老年组).xlsx')
sheet = wb.active
# sheets = wb.sheetnames
dict1 = {}

no_xinli = [286]
sheet = wb.active
data1 =list(sheet.values)
list_bh = data1[0][1:]
#print(list_bh)
del data1[0]

for i in range(1,len(list_bh)+1):
    list2 = []
    
    for item in data1:
        list2.append(item[i])
    dict1[list_bh[i-1]] = list2
#print(dict1)
s = ''
for k,v in dict1.items():
    if str(k) in dict3.keys(): 
        if dict3[str(k)]['sex'] ==1:
            sex = "m"
        else:
            sex = "f"
        dict2 = {}
        list_mx = []
        list_mx.append(f'"Gender":"{sex}"')
        list_mx.append(f'"Age":"O"')
        dict2['surveyId'] = "merge1"
        dict2['name'] = dict3[str(k)]['name'] 
        dict2['code'] = str(k)        
        name = dict3[str(k)]['name'] 
        if k not in no_xinli:
            for i in range(0,30):
                list_mx.append(f'"q{i+1}O":{v[i]}')        
        
        for i in range(1,61):
            if i not in zy:
                list_mx.append(f'"q{i}":{v[29+i]}')
        for i in range(1,27):
            list_mx.append(f'"qv{i}":{v[89+i]}')
        ss = ','.join(list_mx)
        ss ='{'+ss+'}'
        dict2['data'] = ss
        sj = '2023-03-28 20:02:00'
        s= s+f'("merge1","{name}","{str(k)}",\'{ss}\',"{sj}"),'
        #print(dict2)
print(s)

统计网络问卷信息

In [ ]:
filename = 'data/merge1.json'

with open(filename,'r') as fl:
    dict1 = json.load(fl)
#print(dict1)
filename = 'data/杨庙体测明确人员信息(20230328).json'
with open(filename,'r') as fl:
    dict2 = json.load(fl)
dict3 = {}
for k, v in dict1.items():
    print(len(v))
    for item in v:
        phone = item['phone']
        dict3.setdefault(phone,{})
        #dict3[phone]['data'] = v['data']
        for k1,v1 in dict2.items():
            if 'phone' in v1.keys() and phone == v1['phone']:
                dict3[phone]['bh'] = k1
print(dict3)
for k, v in dict2.items():
    for k1, v1 in dict2.items():
        
    if 'bh' not in v.keys():
        print(k)

按照测试编号管理

导入测试人员信息表(符合条件)

In [ ]:
import openpyxl
import json
import time

wb = openpyxl.load_workbook('data/杨庙体测明确人员信息表(20230328).xlsx')
sheet = wb.active
person = {}
for n in range(2, sheet.max_row+1):
    code = int(sheet.cell(n, 1).value)
    person.setdefault(code, {})
    dict1 = {}
    name = sheet.cell(n, 2).value   
    dict1['name'] = name
    dict1['sex'] = sheet.cell(n, 3).value
    dict1['birth'] = str(sheet.cell(n, 4).value).split(' ')[0]
    if sheet.cell(n, 5).value is not None:
        dict1['phone'] = str(sheet.cell(n, 5).value)
    if sheet.cell(n,6).value is not None:
        dict1['unit'] = sheet.cell(n,6).value    
    person[code] = dict1
filename = 'data/杨庙体测明确人员信息(20230328).json'
with open(filename, 'w') as fl:
    json.dump(person, fl, ensure_ascii=False,default=str)
print('ok')

体测报告更名按部门分组

In [ ]:
import os,sys,shutil
import json
import math

filename = 'data/杨庙体测明确人员信息(20230328).json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
old = []
depart = []
for k, v in dict1.items():
    m_depart = v['unit']
    if m_depart not in depart:
        depart.append(m_depart)
        
fi_path = '/data/pdf/ydyb/140/2023-03-29'


# 创建部门办公室    
m_path = 'file/140'
for pn in depart:
    if not os.path.exists(m_path + '/' + pn):
        os.mkdir(m_path + '/' + pn)
fl=os.listdir(fi_path)
for fn in fl:
    if os.path.isfile(fi_path + '/' + fn):
        ofn = int(fn.split('.')[0])
        old.append(ofn) 
old.sort()
for n in old:    
    o_name = f'{fi_path}/{n}.pdf'
    unit = dict1[str(n)]['unit']
    name = dict1[str(n)]['name']
    n_name = f'{m_path}/{unit}/{str(n).rjust(5,"0")}-{name}.pdf'
    #print(o_name,n_name)
    if not os.path.exists(n_name):
        shutil.copyfile(o_name,n_name)
        print(n_name)

生成体检报告情况表

In [ ]:
import os,sys,shutil
import openpyxl
import json

filename = 'data/杨庙体测明确人员信息(20230328).json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
old = []
depart = []
for k, v in dict1.items():
    m_depart = v['unit']
    if m_depart not in depart:
        depart.append(m_depart)
        
fi_path = '/data/pdf/ydyb/140/2023-03-29'
fl=os.listdir(fi_path)
for fn in fl:
    if os.path.isfile(fi_path + '/' + fn):
        ofn = int(fn.split('.')[0])
        old.append(ofn) 
old.sort()
list1 = []
for n in old:
    list2 =[]
    unit = dict1[str(n)]['unit']
    name = dict1[str(n)]['name']
    list2 = [str(n).rjust(5,"0"),name,unit]
    list1.append(list2)

filename = 'data/杨庙体测报告明细表.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
title = ['体测编号','姓名','单位']
sheet.append(title)
for row in list1:
    sheet.append(row)
    
wb.save(filename) 
print('ok!')

生成脊柱问卷信息表

In [28]:
import json
import pymysql
import openpyxl

filename = 'data/杨庙体测明确人员信息(20230328).json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
#print(dict1)
xm = ['qv1','qv2','qv3','qv4','qv5','qv6','qv7','qv8','qv9','qv10','qv11','qv12','qv13','qv14','qv15','qv16','qv17','qv18','qv19','qv20','qv21','qv22','qv23','qv24','qv25','qv26']
title = ['编号','姓名','性别']
for item in xm:
    title.append(item)
db = pymysql.connect(host = "localhost",user = "songyi",password = "yylzs",database = "ydyb" )
cursor = db.cursor()
sql = 'SELECT a.phone ,a.name,a.code,a.data  FROM Survey as a WHERE a.surveyId ="merge1"'
cursor.execute(sql)
results = cursor.fetchall()
list2 =[]
for result in results:    
    c=json.loads(result[3])
    if result[2] is None:
        phone = result[0]
        for k,v in dict1.items():
            if 'phone' in v.keys() and phone ==v['phone']:
                list1 = []
                code = k
                name = v['name']
                list1.append(code)
                list1.append(name)
                list1.append(v['sex'])
                for item in xm:
                    if item in c.keys():
                        list1.append(c[item])
                    else:
                        list1.append('')
                list2.append(list1)
    else:
        list1 = []
        code = result[2]
        name = result[1]
        list1.append(code)
        list1.append(name)
        list1.append(v['sex'])
        for item in xm:
            if item in c.keys():
                list1.append(c[item])
            else:
                list1.append('')   
        list2.append(list1)
filename = 'data/杨庙脊柱问卷明细表.xlsx'
wb = openpyxl.Workbook()
sheet = wb.active
sheet.append(title)
for row in list2:
    sheet.append(row)
    
wb.save(filename) 
print('ok!')      
ok!
In [ ]:
import json
import pymysql

filename = 'data/杨庙体测明确人员信息(20230328).json'
with open(filename,'r') as fl:
    dict1 = json.load(fl)
#print(dict1)
xm = ['qv1','qv2','qv3','qv4','qv5','qv6','qv7','qv8','qv9','qv10','qv11','qv12','qv13','qv14','qv15','qv16','qv17','qv18','qv19','qv20','qv21','qv22','qv23','qv24','qv25','qv26']
db = pymysql.connect(host = "localhost",user = "songyi",password = "yylzs",database = "ydyb" )
cursor = db.cursor()
sql = 'SELECT a.phone ,a.name,a.code,a.data  FROM Survey as a WHERE a.surveyId ="merge1"'
cursor.execute(sql)
results = cursor.fetchall()
for result in results:
    c=json.loads(result[3])
    print(c, type(c))
In [ ]: