Python解析excel文件存入sqlite数据库的方法
一、建立数据库
根据需求建立数据库,建立了两个表,并保证了可以将数据存储到已有的数据库中,代码如下:
import sqlite3 def createDataBase(): cn = sqlite3.connect('check.db') cn.execute('''CREATE TABLE IF NOT EXISTS TB_CHECK (ID integer PRIMARY KEY AUTOINCREMENT, NUMBER INTEGER, ITEM TEXT, REFERENCE TEXT, SUMMARY TEXT, OBJECT TEXT, METHOD TEXT, CONDITION TEXT, VALUE TEXT, RESULT TEXT, SCORE TEXT, REMARKS TEXT, PROVINCE TEXT, TIME TEXT);''') cn.execute('''CREATE TABLE IF NOT EXISTS TB_SCORE (ID integer PRIMARY KEY AUTOINCREMENT, PROVINCE TEXT, TIME TEXT, FILETYPE TEXT, SCORE INTEGER);''') if __name__ == '__main__': createDataBase()
二、使用Python解析excel
Python中的xlrd模块用来解析excel。
相关功能介绍如下:
1. 导入
import xlrd
2. 读取数据
data = xlrd.open_workbook('file.xls')
3. 功能
(1) 通过索引获取
table = data.sheet()[0] table = data.sheet_by_index(0)
(2)通过名称获取
table = data.sheet_by_name(u'sheet1')
(3)获取整行和整列的值(数组)
table.row_values(i) table.col_values(i)
(4)获取行数和列数
nrows = table.nrows ncols = table.ncols
(5)循环行列表数据
for i in range(nrows): print table.row_values(i)
(6)单元格
cell_A1 = table.cell(0,0).value
(7)使用行列索引
cell_A1 = table.cell(0,0).value
练习代码:
import xlrd import xlwt from datetime import date,datetime def read_excel(): # 打开文件 workbook = xlrd.open_workbook(r'file.xls') # 获取所有sheet sheet_name = workbook.sheet_names()[0] sheet = workbook.sheet_by_name(sheet_name) #获取一行的内容 for i in range(6,sheet.nrows): for j in range(0,sheet.ncols): print sheet.cell(i,j).value.encode('utf-8') if __name__ == '__main__': read_excel()
三、Python读取文件名并解析
为了将各个文件的数据加以区分,需要将文件名中标志性字段入库,解析文件的代码如下:
import os def getFileList(dir,wildcard,recursion): os.chdir(dir) fileList = [] check_province = [] check_time = [] file_type = [] exts = wildcard.split(" ") files = os.listdir(dir) for name in files: fullname=os.path.join(dir,name) if(os.path.isdir(fullname) & recursion): getFileList(fullname,wildcard,recursion) else: for ext in exts: if(name.endswith(ext)): fileList.append(name) check_province.append(name.split('-')[1]) check_time.append(name.split('-')[0]) file_type.append(name.split('-')[2]) return fileList,check_time,check_province,file_type
在接下来的使用中 会遇到编码问题 所以在使用这些字段时需要先转码,编写转码函数如下:
#转码函数 def changeCode(name): name = name.decode('GBK') name = name.encode('UTF-8') return name
四、解析excel文件并将其存储到sqlite
Python连接数据库 选取了Python自带的sqlite数据库 相对简单 在此不做太多介绍 如果大家对Python操作sqlite有疑惑的话 个人推荐菜鸟教程~
下面是解析excel文件并存入数据库,其中包含了判断单元格内容:
def readExcel(filename,cn,check_province,check_time,FileType): #读取 workbook = xlrd.open_workbook(filename) # 获取sheet sheet_name = workbook.sheet_names()[0] sheet = workbook.sheet_by_name(sheet_name) check_Item = 'a' itemCount = 0 score = 0 second = sheet.cell(7,1).value.encode('utf-8') for i in range(7,sheet.nrows): if sheet.cell(i,1).value.encode('utf-8') == second: check_Item = sheet.cell(i,0).value.encode('utf-8') continue temp = [] for j in range(0,sheet.ncols): temp.append(sheet.cell(i,j).value.encode('utf-8')) answer = sheet.cell(i,7).value.encode('utf-8') if answer == "yes" or answer == "no": score = score + 1 if answer == "other": print "!!!Failed to import'%s'" % (filename) print "!!!Please Choose an Right Answer for '%s'--------"%(filename) break else: cn.execute("insert into TB_CHECK (ITEM,FIELD,TYPE,CONTENT," "ATTRIBUTE,CHECKPOINT,REMARKS,ANSWER,DESCRIPTION," "SUGGESTION,PROVINCE,TIME,STYLE) " "values('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')" ""%(temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7],temp[8],temp[9],check_province,check_time,check_Item)) itemCount = itemCount + 1 if itemCount != 0: score = round(score * (100 / itemCount), 2) cn.execute("insert into TB_SCORE (PROVINCE,TIME,FILETYPE,SCORE) " "values('%s','%s','%s','%.2f')"%(check_province,check_time,FileType,score)) print "Successful for'%s'--------" % (filename) cn.commit()
整合上述功能:
def importData(path): # 数据库 createDataBase() database = sqlite3.connect("check.db") #文件类型 wildcard = ".xls" list = getFileList(path,wildcard,1) nfiles = len(list[0]) #文件名 file = list[0] #时间 time = list[1] #省份 province = list[2] # #文件类型 FileType = list[3] for count in range(0,nfiles): filename = file[count] check_province = changeCode(province[count]) check_time = time[count] File_type = changeCode(FileType[count]) readExcel(filename,database,check_province,check_time,File_type) if __name__ == '__main__': if len(sys.argv) != 2: print "Wrong Parameters" else: path = sys.argv[1] importData(path)
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家学习或者使用python能有所帮助,如果有疑问大家可以留言交流。
相关推荐
bleach00 2020-11-10
林德强之原创 2020-08-13
PlumRain 2020-08-03
bob于 2020-07-26
dataminer 2020-06-25
neverstopforcode 2020-06-18
zxznsjdsj 2020-06-16
langyue 2020-06-13
huangyx 2020-06-11
鲁氏汤包王 2020-06-11
LWLWLiang 2020-05-28
wkwanglei 2020-05-26
Coohx 2020-04-13
thunderstorm 2020-05-09
Andrea0 2020-05-04
数据库工具开发 2020-04-25
tlsmile 2020-04-24
helencoder 2020-04-10