# 需求分析:# 1、读出stu.xls文件里的内容# 2、判断,如果是1用已交作业表示,如果是空用未交作业表示# 3、拼接sql# 4、插入到数据库里的jxz_stu表里,四个字段为name,c1,c2,c3
def con_mysql(sql): import pymysql conn = pymysql.connect(host = ‘211.149.218.16‘,user = ‘jxz‘,password = ‘123456‘,db = ‘jxz‘,charset = ‘utf8‘)
cur = conn.cursor() cur.execute(sql) res = cur.fetchall() cur.close() conn.close() return res
def is_send(a): # 判断是否交作业了 if a: return ‘已交作业‘ else: return ‘未交作业‘
def readexcel(filename): import xlrd book = xlrd.open_workbook(filename) # 打开指定的excel表 sheet = book.sheet_by_name(‘Sheet1‘) # 读取Sheet1页的内容
# print(sheet.nrows) # 打印出excel表里sheet1页的行数
# print(sheet.ncols) # 打印出excel表里sheet1页的列数
# print(sheet.cell(1,1)) # 获取第1行,第1列的值,打印出text:‘沙河‘
# print(sheet.cell(1,1).value) # 获取到沙河 for i in range(sheet.nrows): # 按照行循环 lines= sheet.row_values(i) # 每行的内容 name = lines[0] c1 = is_send(lines[1]) c2 = is_send(lines[2]) c3 = is_send(lines[3]) sql = ‘insert into jxz_stu values("{name}","{c1}","{c2}","{c3}";)‘.format(name = name,c1 = c1,c2 = c2,c3 = c3)
con_mysql(sql)readexcel(‘stu.xls‘)
原文地址:https://www.cnblogs.com/laosun0204/p/8544937.html
时间: 2024-10-29 10:58:33