Files
gaokao/excel文件操作.ipynb
T
512song ad0d0a5396 jupyterlab
jupyterlab项目
2021-03-07 19:16:41 +08:00

9.4 KiB

In [ ]:
import csv
import pymysql

def read_data(filename,re_id):
    detail = {}
    with open(filename) as f:
        reader = csv.reader(f)
        header_row =next(reader)
        for row in reader:
            detail.setdefault(row[0],)
            detail[row[0]].append((re_id,row[0],row[2]))
    return detail

per_id = 1
item_id = 1
re_date = '2020-07-07'
db = pymysql.connect("localhost","songyi","yylzs","mydata" )
cursor = db.cursor()
filename = '户外跑步数据.csv'
sql = "select id from sports_record where re_date=%s and item_id =%s and person_id =%s"
cursor.execute(sql, (re_date,item_id,per_id))
result = cursor.fetchone()
if result:
    print('记录已经存在!')
else:
    sql = 'insert into sports_record (re_date,item_id,person_id) values(%s,%s,%s)'
    cursor.execute(sql,(re_date,item_id,per_id))
    db.commit()
    re_id = cursor.lastrowid;
    print(re_id)
    detail = read_data(filename,re_id)
    sql = "insert into sports_detail (rec_id_id,target_id_id,value) values(%s,%s,%s)"
    try:
        cursor.executemany(sql,detail)
        db.commit()
        print("ok!")
    except:
       # 如果发生错误则回滚
       db.rollback()   
db.close()

    
In [ ]:
choice = input('记录已存在,是否覆盖?(y/n)')
if choice.upper() == "Y":
    print('记录已更新')
In [ ]:
import csv
import pymysql
def read_data(filename,re_id):
    detail = []
    with open(filename) as f:
        reader = csv.reader(f)
        header_row =next(reader)
        for row in reader:
            detail.append((re_id,row[0],row[2]))
    return detail
    
    
per_id = 1
item_id = 1
re_date = '2020-06-28'
db = pymysql.connect("localhost","songyi","yylzs","mydata" )
cursor = db.cursor()
filename = '户外跑步数据.csv'
re_id = 19;
detail = read_data(filename,re_id)
print(detail)
       
db.close()
In [32]:
import openpyxl
import pymysql
import json

db = pymysql.connect("81.68.135.145","colab","songyi","gaokao" )
cursor = db.cursor()
sql = 'select code from college';
cursor.execute(sql)
results = cursor.fetchall()
college = []
for result in results:
    college.append(result[0])
wb = openpyxl.load_workbook('./data/2017-2019.xlsx')
#sheet = wb.active
sheets = wb.sheetnames
new_col = []
dict1 = {}
new_code = []
for m in sheets:
    sheet = wb[m]
    
    
    for n in range(4,sheet.max_row):
        col_code = sheet.cell(n,1).value
        
        if col_code not in college and col_code not in new_code: 
            m_year = []
            dict2 = {}
            #dict2.setdefault('nian',[])
            new_code.append(col_code)            
            dict2['name'] =  sheet.cell(n,2).value
            dict2['nian'] =  []
            #m_year.append(m[0:4])
            #dict2['nian'][] = (m[0:4])
            dict1[col_code] = dict2
    for m_code in dict1.keys():
        if m[0:4] not in dict1[m_code]['nian']:
            dict1[m_code]['nian'].append(m[0:4])
    
filename = './data/2020年未招生学校.json'
with open(filename,'w') as fl:
    json.dump(dict1, fl,ensure_ascii=False)
            
            
            
#            new_col.append((col_code,sheet.cell(n,2).value,m[0:4]))
           # print(col_code)

print('ok!')
ok!
In [ ]:
### 导入2020年投档录取信息
import pymysql
import json
db = pymysql.connect("81.68.135.145","colab","songyi","gaokao" )
cursor = db.cursor()
sql = "select * from digao_2020"
cursor.execute(sql)
results = cursor.fetchall()
college = []
m_adm = []
for result in results:
    bm_col = result[1][0:4]
    bm_adm = result[2][0:2]
    name_adm = result[2][2:]
    if bm_col not in college:
        college.append(bm_col)        
        
    m_adm.append((bm_col,bm_adm,result[3],result[4],result[5],result[6],result[7],'2020'))   
sql = "insert into admission (college,speciality,plan,plan_dispense,num_dispense,num_min,rank_min,nian) values(%s,%s,%s,%s,%s,%s,%s,%s)"
try:
    cursor.executemany(sql,m_adm)
    db.commit()
    print("ok!")
except:
    # 如果发生错误则回滚
    print("error!")
    db.rollback()   

db.close()
In [ ]:
### 导入2017-2019年投档录取信息
import pymysql
import json
db = pymysql.connect("81.68.135.145","colab","songyi","gaokao" )
cursor = db.cursor()
sql = "select * from digao_1719"
cursor.execute(sql)
results = cursor.fetchall()
college = []
m_adm = []
for result in results:
    bm_col = result[1][0:4]
    bm_adm = result[2][0:2]
    name_adm = result[2][2:]
    if bm_col not in college:
        college.append(bm_col)        
        
    m_adm.append((bm_col,bm_adm,result[3],result[4],result[5],result[6],result[7],'2020'))   
sql = "insert into admission (college,speciality,plan,plan_dispense,num_dispense,num_min,rank_min,nian) values(%s,%s,%s,%s,%s,%s,%s,%s)"
try:
    cursor.executemany(sql,m_adm)
    db.commit()
    print("ok!")
except:
    # 如果发生错误则回滚
    print("error!")
    db.rollback()   

db.close()

合并excel文件

In [ ]:
import openpyxl
import json

for i in range(1,23):
    fl_name = '识别结果'
wb = openpyxl.load_workbook('./data/2017-2019.xlsx')
#sheet = wb.active
sheets = wb.sheetnames
new_col = []
dict1 = {}
new_code = []
for m in sheets:
    sheet = wb[m]
    
    
    for n in range(4,sheet.max_row):
        col_code = sheet.cell(n,1).value
        
        if col_code not in college and col_code not in new_code: 
            m_year = []
            dict2 = {}
            #dict2.setdefault('nian',[])
            new_code.append(col_code)            
            dict2['name'] =  sheet.cell(n,2).value
            dict2['nian'] =  []
            #m_year.append(m[0:4])
            #dict2['nian'][] = (m[0:4])
            dict1[col_code] = dict2
    for m_code in dict1.keys():
        if m[0:4] not in dict1[m_code]['nian']:
            dict1[m_code]['nian'].append(m[0:4])
    
filename = './data/2020年未招生学校.json'
with open(filename,'w') as fl:
    json.dump(dict1, fl,ensure_ascii=False)
            
            
            
#            new_col.append((col_code,sheet.cell(n,2).value,m[0:4]))
           # print(col_code)

print('ok!')
In [ ]: