Python讀取Excel的方法實例分析
本文實例講述了Python讀取Excel的方法。分享給大家供大家參考。具體如下:
今天需要從一個Excel文檔(.xls)中導(dǎo)數(shù)據(jù)到數(shù)據(jù)庫的某表,開始是手工一行行輸?shù)摹:髞硐氩荒芤恢边@樣,就用Python寫了下面的代碼,可以很方便應(yīng)對這種場景。比如利用我封裝的這些方法可以很方便地生成導(dǎo)入數(shù)據(jù)的SQL。 當(dāng)然熟悉Excel編程的同學(xué)還可以直接用VBA寫個腳本生成插入數(shù)據(jù)的SQL。
還可以將.xls文件改為.csv文件,然后通過SQLyog或者Navicat等工具導(dǎo)入進(jìn)來,但是不能細(xì)粒度控制(比如不滿足某些條件的某些數(shù)據(jù)不需要導(dǎo)入,而用程序就能更精細(xì)地控制了;又比如重復(fù)數(shù)據(jù)不能重復(fù)導(dǎo)入;還有比如待導(dǎo)入的Excel表格和數(shù)據(jù)庫中的表的列不完全一致) 。
我的Python版本是3.0,需要去下載xlrd 3: http://pypi.python.org/pypi/xlrd3/ 然后通過setup.py install命令安裝即可
import xlrd3
'''
author: jxqlove?
本代碼主要封裝了幾個操作Excel數(shù)據(jù)的方法
'''
'''
獲取行視圖
根據(jù)Sheet序號獲取該Sheet包含的所有行,返回值類似[ ['a', 'b', 'c'], ['1', '2', '3'] ]
sheetIndex指示sheet的索引,0表示第一個sheet,依次類推
xlsFilePath是Excel文件的相對或者絕對路徑
'''
def getAllRowsBySheetIndex(sheetIndex, xlsFilePath):
workBook = xlrd3.open_workbook(xlsFilePath)
table = workBook.sheets()[sheetIndex]
rows = []
rowNum = table.nrows # 總共行數(shù)
rowList = table.row_values
for i in range(rowNum):
rows.append(rowList(i)) # 等價于rows.append(i, rowLists(i))
return rows
'''
獲取某個Sheet的指定序號的行
sheetIndex從0開始
rowIndex從0開始
'''
def getRow(sheetIndex, rowIndex, xlsFilePath):
rows = getAllRowsBySheetIndex(sheetIndex, xlsFilePath)
return rows[rowIndex]
'''
獲取列視圖
根據(jù)Sheet序號獲取該Sheet包含的所有列,返回值類似[ ['a', 'b', 'c'], ['1', '2', '3'] ]
sheetIndex指示sheet的索引,0表示第一個sheet,依次類推
xlsFilePath是Excel文件的相對或者絕對路徑
'''
def getAllColsBySheetIndex(sheetIndex, xlsFilePath):
workBook = xlrd3.open_workbook(xlsFilePath)
table = workBook.sheets()[sheetIndex]
cols = []
colNum = table.ncols # 總共列數(shù)
colList = table.col_values
for i in range(colNum):
cols.append(colList(i))
return cols
'''
獲取某個Sheet的指定序號的列
sheetIndex從0開始
colIndex從0開始
'''
def getCol(sheetIndex, colIndex, xlsFilePath):
cols = getAllColsBySheetIndex(sheetIndex, xlsFilePath)
return cols[colIndex]
'''
獲取指定sheet的指定行列的單元格中的值
'''
def getCellValue(sheetIndex, rowIndex, colIndex, xlsFilePath):
workBook = xlrd3.open_workbook(xlsFilePath)
table = workBook.sheets()[sheetIndex]
return table.cell(rowIndex, colIndex).value # 或者table.row(0)[0].value或者table.col(0)[0].value
if __name__=='__main__':
rowsInFirstSheet = getAllRowsBySheetIndex(0, './產(chǎn)品.xls')
print(rowsInFirstSheet)
colsInFirstSheet = getAllColsBySheetIndex(0, './產(chǎn)品.xls')
print(colsInFirstSheet)
print(getRow(0, 0, './產(chǎn)品.xls'))
# 獲取第一個sheet第一行的數(shù)據(jù)
print(getCol(0, 0, './產(chǎn)品.xls'))
# 獲取第一個sheet第一列的數(shù)據(jù)
print(getCellValue(0, 3, 2, './產(chǎn)品.xls'))
# 獲取第一個sheet第四行第二列的單元格的值
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
Python實現(xiàn)字典按照value進(jìn)行排序的方法分析
這篇文章主要介紹了Python實現(xiàn)字典按照value進(jìn)行排序的方法,結(jié)合實例形式分析了Python字典按照value進(jìn)行排序的相關(guān)操作技巧,需要的朋友可以參考下2017-12-12
Python函數(shù)參數(shù)分類使用與新特性詳細(xì)分析講解
在聲明函數(shù)的時候,一般會根據(jù)函數(shù)所要實現(xiàn)的功能來決定函數(shù)是否需要參數(shù)。在多數(shù)情況下,我們聲明的函數(shù)都會使用到參數(shù),這篇文章主要介紹了Python函數(shù)參數(shù)2023-01-01
pycharm如何使用anaconda中的各種包(操作步驟)
這篇文章主要介紹了pycharm如何使用anaconda中的各種包,本文通過操作步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07

