目标:1个excel表内容导入到数据库中,例如:原始excel文件为 aaa.xls

首先:将aaa.xls 转换成aaa.txt ,注意当文件中含有中文字符时,可以通过notepad++打开,在“格式”下选择转存为utf8格式或者选择utf8格式再另存

即:

txtFile = aaa.txt

#引入MySQLdb库,这个库的安装再另外写

import MySQLdb

#定义数据库的接入

def getDbAccessConf( confFileName ):

dbIp = "unknown"
dbUser = "unknown"
dbPasswd = "unknown"
dbName = "unknown"

patternDbIp = r"(.*)dbIp(.*)=(.*)"
patternDbUser = r"(.*)dbUser(.*)=(.*)"
patternDbPasswd = r"(.*)dbPasswd(.*)=(.*)"
patternDbName = r"(.*)dbName(.*)=(.*)"

fileData = open(confFileName)
lines = fileData.readlines()

for line in lines:
matchObj = re.match( patternDbIp, line )
if matchObj:
dbIp = matchObj.group(3).strip()

matchObj = re.match( patternDbUser, line )
if matchObj:
dbUser = matchObj.group(3).strip()

matchObj = re.match( patternDbPasswd, line )
if matchObj:
dbPasswd = matchObj.group(3).strip()

matchObj = re.match( patternDbName, line )
if matchObj:
dbName = matchObj.group(3).strip()

return dbIp, dbUser, dbPasswd, dbName

#定义数据内容的插入
def insertIntoDb( db ):
tableHeadMobile = 'mobileHead'
idxRow = 0;
cursor = db.cursor()

f= open('ccc.txt','r')
for row in f:
contents = row.split('\t')
operator = contents[0]
#print '\n ope: ' + operator,
for idx in range( 1, len(contents) ):
headMobile = contents[idx]
if "" == headMobile:
print 'headMobile is empty. head=' + headMobile
continue

sqlComm = 'select count(id) from ' + tableHeadMobile + ' where headMobile = "' + headMobile + '"'
cursor.execute( sqlComm )
sqlData = cursor.fetchall()
rowNum = sqlData[0][0]
if 0 == rowNum:
sqlComm = 'insert into ' + tableHeadMobile + ' values( "' + str(idxRow) + '", "' + operator + '", "' + headMobile + '" )'
print sqlComm
cursor.execute( sqlComm )
idxRow = idxRow + 1

db.commit()
cursor.close()

最新文章

  1. Ruby Gem命令详解
  2. (转)Java集合框架:HashMap
  3. Go-Agent部署与FQ教程(2016-10-28)
  4. 使用Word发布文章到 WordPress 博客
  5. Oracle 11g AWR和ADDM性能报告
  6. [ZOJ 3839] Poker Face (递归)
  7. linux下,如何把整个文件夹上传到服务器(另一台linux)
  8. Delphi中使用python脚本读取Excel数据
  9. mysql show命令
  10. Java学习笔记 11/15:一个简单的JAVA例子
  11. POJ1222熄灯问题
  12. 【记录】IntelliJ IDEA—IDEA2018-2019激活
  13. Hadoop记录-Hadoop NameNode 高可用 (High Availability) 实现解析
  14. China’s movie heroes 《红海行动》展现中国英雄本色
  15. SQLite使用入门
  16. python的return
  17. ECMAScript5严格模式
  18. “凯易迅Calix”实习上机——求元音开头辅音结尾的子串
  19. Qt上FFTW組件的编译与安裝
  20. Understanding Tensorflow using Go

热门文章

  1. 菜鸟笔记 -- Chapter 1 计算机从0到1
  2. aes 加密,解密(2)
  3. leetcode笔记(六)740. Delete and Earn
  4. Hello,移动WEB—Viewport_Meta标签
  5. koa2 mongdb 做后端接口的小demo
  6. 利用phpspreadsheet切割excel大文件
  7. css文本截字,超出文本省略号显示
  8. Tensorflow的最佳实践
  9. ISE中FPGA的实现流程
  10. MySQL分区的限制(最多有多少个分区)