文章标题
来源:互联网 发布:淘宝手机助手安卓版 编辑:程序博客网 时间:2024/06/05 15:59
用python把excel导入mysql
# -*- encoding:utf-8 -*-import xlrdimport MySQLdbimport datetime import sysimport stringimport rereload(sys)sys.setdefaultencoding('utf8') #打开xlswb=xlrd.open_workbook("test.xlsx")sheet=wb.sheet_by_index(0)#连接数据库db=MySQLdb.connect(host="localhost",user="root",passwd="toor",db="test",charset="utf8")#获取游标cursor = db.cursor()#检测表是否存在,存在删除cursor.execute(' drop table if exists test;')cursor.execute("create table test(id int auto_increment primary key not null," "date varchar(255),time varchar(255),classes varchar(255),subject varchar(255),people varchar(255)," "place1 varchar(255),place2 varchar(255),ty varchar(255))")#获取数据for i in range(3,sheet.nrows): if(type(date)==str #若excel时间的格式为 date=sheet.row_values(i)[1] else if(type(date)==float) #若excel时间的格式为float date=xlrd.xldate.xldate_as_datetime(sheet.row_values(i)[1],0).strftime('%Y-%m-%d') time=sheet.row_values(i)[1] classes=str(sheet.row_values(i)[2]) subject=sheet.row_values(i)[3] people=str(sheet.row_values(i)[4]) place1=sheet.row_values(i)[6] place2=str(sheet.row_values(i)[7]) ty=sheet.row_values(i)[8] #正则匹配 people=re.sub(r'.0$'," ",people) classes=re.sub(r'.0$'," ",classes) place2=re.sub(r'.0$'," ",place2) #执行导入 cursor.execute('insert into test(date,time,classes,subject,people,place1,place2,ty)VALUES ("%s","%s","%s","%s","%s","%s","%s","%s") ' %(date,time,classes,subject,people,place1,place2,ty))#提交 db.commit()#关闭数据库连接db.close()#打印记录print " "print "Done!"print " "columns =str(sheet.ncols)rows= str(sheet.nrows)print ("%s line %s columns "%(rows,columns))
——————注释—————
设置编码
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0x?? in position 1: ordinal not in range(128),python没办法处理非ascii编码的,此时需要自己设置将python的默认编码,一般设置为utf8的编码格式。
关于xlrd模块使用
http://www.mamicode.com/info-detail-1401491.html
excel转换sql时间设置
为什么要转呢?
python读取excel时间的格式可能为str,也可能为float
float转str时间可参考
http://www.cnblogs.com/cathouse/archive/2012/11/19/2777678.html
http://www.cnblogs.com/kfx2007/p/6029356.html
正则匹配
为什么要用正则?
python读取excel内的数字,自动将int型转化为float
eg:
1102->1102.0
950->950.0
用正则匹配到.0后替换为空 str = (r’.0$’,” “,str)
———-遇到的坑———–
1.已经编写了如果存在就删出数据表的代码,结果还是报错显示:存在表,
第一次执行报错,在执行一次就好,什么原因我也不知道。
2.打开一个从sql导出的excel,报错显示
3.读取excel时遇到空行直接会 unicode 报错
4.用py执行数据表合并,成功执行但并未回显
尝试语句:
select * from table1 union all select * from table2 select * from table1,table2select * from table1,table2 as A
5.正则匹配遇到的坑
———-总结——–
1.边学边做伤不起!
2.本想debug完后写,但测试发现都忘的差不多了。
3.不能等bug积攒起来解决,有问题一定当时解决。
4.水平有限,欢迎指正。
- 文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题 文章标题 文章标题 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- 文章标题
- eclipse中插件windowBuilder的使用
- php关键字区分
- Android_View与Activity的转化
- list四种遍历方式
- 剑指Offer(第二版)面试题16:数值的整数次方
- 文章标题
- 中文乱码+eclipse
- 【DRP】图片上传(第二种)
- mybatis集成spring
- C++释放类的静态指针成员
- MTK 功能机编译
- Neutron GRE模式要注意的问题
- 5. 变量(Variables)
- Android Tombstone 分析