使用python将excel数据导入数据库过程详解
来源:中文源码网 浏览: 次 日期:2019年11月5日
【下载文档: 使用python将excel数据导入数据库过程详解.txt 】
(友情提示:右键点上行txt文档名->目标另存为)
使用python将excel数据导入数据库过程详解因为需要对数据处理,将excel数据导入到数据库,记录一下过程。使用到的库:xlrd 和 pymysql (如果需要写到excel可以使用xlwt)直接丢代码,使用python3,注释比较清楚。import xlrd
import pymysql
# import importlib
# importlib.reload(sys) #出现呢reload错误使用
def open_excel():
try:
book = xlrd.open_workbook("XX.xlsx") #文件名,把文件与py文件放在同一目录下
except:
print("open excel file failed!")
try:
sheet = book.sheet_by_name("sheet名称") #execl里面的worksheet1
return sheet
except:
print("locate worksheet in excel failed!")
#连接数据库
try:
db = pymysql.connect(host="127.0.0.1",user="root",
passwd="XXX",
db="XXX",
charset='utf8')
except:
print("could not connect to mysql server")
def search_count():
cursor = db.cursor()
select = "select count(id) from XXXX" #获取表中xxxxx记录数
cur