溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Python3怎么讀取Excel數(shù)據(jù)存入MySQL

發(fā)布時間:2021-05-22 15:56:48 來源:億速云 閱讀:235 作者:Leah 欄目:開發(fā)技術(shù)

使用Python3怎么讀取Excel數(shù)據(jù)存入MySQL?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

Python是數(shù)據(jù)分析的強大利器。

利用Python做數(shù)據(jù)分析,第一步就是學(xué)習(xí)如何讀取日常工作中產(chǎn)生各種excel報表并存入數(shù)據(jù)中,方便后續(xù)數(shù)據(jù)處理。

這里向大家分享python3如何使用xlrd讀取excel,并使用Python3操作pymysql模塊將數(shù)據(jù)存入Mysql中,有需要的朋友們一起來看看吧。

前言

pymsql是Python中操作MySQL的模塊,其使用方法和MySQLdb幾乎相同。但目前pymysql支持python3.x而后者不支持3.x版本。

python操作excel主要用到xlrd和xlwt這兩個庫,即xlrd是讀excel,xlwt是寫excel的庫。

版本

python >= 3.6

mysql >= 5.7.19

安裝

python、mysql的安裝這里就不詳細述說了,有需要的朋友自行百度

xlrd : 可以使用pip安裝也可手動下載源碼安裝,pip安裝:pip install xlrd

pymysql : 可以使用pip安裝也可手動下載源碼安裝, 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盤中名為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ù)庫連接

db = pymysql.connect("localhost", "root", "gaishi123", "sales_data", use_unicode=True, charset="utf8")

gaishi123是mysql的root的密碼,sales_data是數(shù)據(jù)庫名

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))     # 銷量
   quantity_ticket = int(table_one.cell_value(nrows_one, 2))   # 票數(shù)
   rate_electronic = float(table_one.cell_value(nrows_one, 3))  # 電子直銷占比
   sales_thanlastweek = float(table_one.cell_value(nrows_one, 4))  # 銷量同比上周
   sales_thanlastyear = float(table_one.cell_value(nrows_one, 5))  # 銷量同比去年
   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))     # 訪問量
   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)) # 訪問量同比上周
   break
 # 將數(shù)據(jù)存入數(shù)據(jù)庫
 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)建一個游標對象 cursor
  cursor = db.cursor()
  cursor.execute(sql)
 except Exception as e:
  # 發(fā)生錯誤時回滾
  db.rollback()
  print(str(e))
 else:
  db.commit() # 事務(wù)提交
  print('事務(wù)處理成功')

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI