使用Python3怎么讀取Excel數(shù)據(jù)存入MySQL?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
Python是數(shù)據(jù)分析的強(qiáng)大利器。
利用Python做數(shù)據(jù)分析,第一步就是學(xué)習(xí)如何讀取日常工作中產(chǎn)生各種excel報(bào)表并存入數(shù)據(jù)中,方便后續(xù)數(shù)據(jù)處理。
這里向大家分享python3如何使用xlrd讀取excel,并使用Python3操作pymysql模塊將數(shù)據(jù)存入Mysql中,有需要的朋友們一起來(lái)看看吧。
前言
pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同。但目前pymysql支持python3.x而后者不支持3.x版本。
python操作excel主要用到xlrd和xlwt這兩個(gè)庫(kù),即xlrd是讀excel,xlwt是寫(xiě)excel的庫(kù)。
版本
python >= 3.6
mysql >= 5.7.19
安裝
python、mysql的安裝這里就不詳細(xì)述說(shuō)了,有需要的朋友自行百度
xlrd : 可以使用pip安裝也可手動(dòng)下載源碼安裝,pip安裝:pip install xlrd
pymysql : 可以使用pip安裝也可手動(dòng)下載源碼安裝, pip安裝: pip install xlrd
模塊
import xlrd import pymysql from datetime import datetime from xlrd import xldate_as_tuple
讀取excel
data = xlrd.open_workbook("D:/sales_data.xls") //讀取D盤(pán)中名為sales_data的excel表格 table_one = data.sheet_by_index(0) //根據(jù)sheet索引獲取sheet的內(nèi)容 table_two = data.sheet_by_index(1)
創(chuàng)建數(shù)據(jù)庫(kù)連接
db = pymysql.connect("localhost", "root", "gaishi123", "sales_data", use_unicode=True, charset="utf8")
gaishi123是mysql的root的密碼,sales_data是數(shù)據(jù)庫(kù)名
for site in sites: # 遍歷sheet1 for nrows_one in range(1, int(table_one.nrows)): if table_one.cell_value(nrows_one, 0) == site: payday = table_one.cell_value(0, 8) date = datetime(*xldate_as_tuple(payday, 0)) payday = date.strftime('%Y/%m/%d') # 出票日期 sales = float(table_one.cell_value(nrows_one, 1)) # 銷(xiāo)量 quantity_ticket = int(table_one.cell_value(nrows_one, 2)) # 票數(shù) rate_electronic = float(table_one.cell_value(nrows_one, 3)) # 電子直銷(xiāo)占比 sales_thanlastweek = float(table_one.cell_value(nrows_one, 4)) # 銷(xiāo)量同比上周 sales_thanlastyear = float(table_one.cell_value(nrows_one, 5)) # 銷(xiāo)量同比去年 break # 遍歷sheet2 for nrows_two in range(1, int(table_two.nrows)): if table_one.cell_value(nrows_two, 0) == site: session = int(table_two.cell_value(nrows_two, 1)) # 訪(fǎng)問(wèn)量 rate_conversion = float(table_two.cell_value(nrows_two, 2)) # 轉(zhuǎn)化率 rate_paysuccess = float(table_two.cell_value(nrows_two, 3)) # 支付成功率 session_thanlastweek = float(table_two.cell_value(nrows_two, 4)) # 訪(fǎng)問(wèn)量同比上周 break # 將數(shù)據(jù)存入數(shù)據(jù)庫(kù) sql = "insert into sales_data(SITE, PAYDAY, SALES, QUANTITY_TICKET, RATE_ELECTRONIC, SALES_THANLASTWEEK," \ "SALES_THANLASTYEAR, SESSION, SESSION_THANLASTWEEK, RATE_CONVERSION, RATE_PAYSUCCESS)" \ " values ('%s','%s', %f, %d, %f, %f, %f, %d, %f, %f, %f)" %\ (site, payday, sales, quantity_ticket, rate_electronic, sales_thanlastweek, sales_thanlastyear, session, session_thanlastweek, rate_conversion, rate_paysuccess) try: # 使用 cursor() 方法創(chuàng)建一個(gè)游標(biāo)對(duì)象 cursor cursor = db.cursor() cursor.execute(sql) except Exception as e: # 發(fā)生錯(cuò)誤時(shí)回滾 db.rollback() print(str(e)) else: db.commit() # 事務(wù)提交 print('事務(wù)處理成功')
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
當(dāng)前文章:使用Python3怎么讀取Excel數(shù)據(jù)存入MySQL-創(chuàng)新互聯(lián)
文章分享:http://www.chinadenli.net/article46/dcigeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、ChatGPT、虛擬主機(jī)、標(biāo)簽優(yōu)化、建站公司、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
全網(wǎng)營(yíng)銷(xiāo)推廣知識(shí)