码迷,mamicode.com
首页 > 数据库 > 详细

使用pymysql 存储xls

时间:2015-08-10 14:59:06      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

#-*- coding: utf-8 -*-
__author__ = ‘Work‘
import sys
reload(sys)
sys.setdefaultencoding("utf-8")
import xlrd
import pymysql

conn = pymysql.connect(host=‘192.168.12.115‘, port=3306, user=‘root‘, passwd=‘123456‘, db=‘captchadb‘,charset=‘utf8‘)
cur = conn.cursor()

data = xlrd.open_workbook(‘E:\Captcha.xls‘)
table = data.sheets()[0]

for row in range(table.nrows):
    cellQ = table.cell(row, 0).value
    cellA = table.cell(row, 1).value
    cellB = table.cell(row, 2).value
    cellC = table.cell(row, 3).value
    cellD = table.cell(row, 4).value
    print row,cellQ,cellA,cellB,cellC,cellD
    strsql = ‘insert into captchatable(question, choiceA, choiceB, choiceC, answer) value (\‘%s\‘,\‘%s\‘,\‘%s\‘,\‘%s\‘,\‘%d\‘)‘ % (cellQ,cellA,cellB,cellC,cellD)
    print strsql
    cur.execute(strsql)
conn.commit()
cur.close()
conn.close()



使用pymysql 存储xls

标签:

原文地址:http://my.oschina.net/u/2007546/blog/490163

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!