zoukankan      html  css  js  c++  java
  • Python基础-读取excel

    import xlrd
    book = xlrd.open_workbook('lanxia.xls')
    sheet = book.sheet_by_name('sheet1')
    rows = sheet.nrows#sheet页里面的行数
    clos = sheet.ncols#sheet页里面的列数
    print(sheet.cell(1,1).value)#通过指定行和列去获取到单元格里面的内容
    row_data = sheet.row_values(1)#获取第一行的内容
    for i in range(rows):
    print(sheet.row_values(i))#获取第几行的数据
    #1、先读excel
    #2、读一行,拼一条sql语句
    # insert into jxz_stu  (name,c1,c2,c3) values ('牛寒阳','交','交','交');
    #3、插入数据库
    
    def conn_mysql(sql):
        import pymysql
        conn = pymysql.connect(host='211.149.218.16',user='jxz',password='123456',db='jxz',charset='utf8')
        cur = conn.cursor(cursor=pymysql.cursors.DictCursor)
        cur.execute(sql)
        res = cur.fetchone()
        conn.commit()
        cur.close()
        conn.close()
        return res
    def is_send(a):
        if a:
            return '交了'
        return '未交'
    
    def readExcel(file_name):
        book = xlrd.open_workbook(file_name)
        sheet = book.sheet_by_name('Sheet1')
        for i in range(sheet.nrows):
            line = sheet.row_values(i)
            name = line[0]
            c1 = is_send(line[1])
            c2 = is_send(line[2])
            c3 = is_send(line[3])
            sql = "insert into jxz_stu values('{name}','{c1}','{c2}','{c3}');".format(name=name,c1=c1,c2=c2,c3=c3)
            conn_mysql(sql)
    readExcel('stu.xlsx')
  • 相关阅读:
    redis该怎么用
    cookie和session的比较
    web常见攻击
    请大神指导从大日志文件中统计关键字次数的办法
    apache中 MaxClients 与MaxRequestsPerChild
    如何提高缓存命中率
    CSU-ACM2018暑假集训比赛1
    CodeForces
    CodeForces
    CodeForces
  • 原文地址:https://www.cnblogs.com/niuniu2018/p/7828542.html
Copyright © 2011-2022 走看看