我是在win7下读取的。 python版本是:3.5
import xlrd
import re
import sqlite3
def read_xlsx():
workbook = xlrd.open_workbook('E:\')
booksheet = workbook.sheet_by_name('Sheet1')
p = list()
for row in range():
row_data = []
for col in range():
cel = (row, col)
val =
try:
val =
val = (r'\s+', '', val)
except:
pass
if type(val) == float:
val = int(val)
else:
val = str( val )
row_data.append(val)
(row_data)
return p
def operat_sqlite(*data):
# print(type(data))
# print(data)
print(data[0])
try:
conn = ('E:\')
except:
print('open sqlite3 failed.')
return
else: #操作数据库
c = ()
for item in data:
for i in range(len(item)):
DLDMv = item[i][1]
LDDMv = item[i][3]
LDMCv = item[i][2]
FHSSLXv = item[i][5]
XZQHv = item[i][6]
try:
#creat sql
("insert into roadkey (DLDM, LDDM, LDMC, FHSSLX, XZQH) values (?, ?, ?, ?, ?)", (DLDMv, LDDMv, LDMCv, FHSSLXv, XZQHv))
()
except:
print('insert roadky failed ')
pass
print(i)
print(item[i])
()
return
if __name__ == '__main__':
data_list = list()
data_list = read_xlsx()
operat_sqlite(data_list)
1. 代码功能:读取xlsx文件内容,插入到sqlite3数据库里面。
2. 安装xlrd: pip install xlrd。
sqlite3已经有库了。。不用安装,直接使用。
参考网址:Python xlsx读取
python 操作Excel读写
python开发_sqlite3_完整