這篇文章主要介紹了怎么使用python3+xlrd解析Excel的實(shí)例,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

Python是一種跨平臺(tái)的、具有解釋性、編譯性、互動(dòng)性和面向?qū)ο蟮哪_本語言,其最初的設(shè)計(jì)是用于編寫自動(dòng)化腳本,隨著版本的不斷更新和新功能的添加,常用于用于開發(fā)獨(dú)立的項(xiàng)目和大型項(xiàng)目。
實(shí)例如下所示:
# -*- coding: utf-8 -*-
import xlrd
def open_excel(file = 'file.xls'):#打開要解析的Excel文件
try:
data = xlrd.open_workbook(file)
return data
except Exception as e:
print(e)
def excel_by_index(file = 'file.xls', colindex = 0, by_index = 0):#按表的索引讀取
data = open_excel(file)#打開excel文件
tab = data.sheets()[by_index]#選擇excel里面的Sheet
nrows = tab.nrows#行數(shù)
ncols = tab.ncols#列數(shù)
colName = tab.row_values(colindex)#第0行的值
list = []#創(chuàng)建一個(gè)空列表
for x in range(0, nrows):
row = tab.row_values(x)
if row:
app = {}#創(chuàng)建空字典
for y in range(0, ncols):
app [ colName[y] ] = row[y]
list.append(app)
return list
def read_excel(file = 'file.xls', by_index = 0):#直接讀取excel表中的各個(gè)值
data = open_excel(file)#打開excel文件
tab = data.sheets()[by_index]#選擇excel里面的Sheet
nrows = tab.nrows#行數(shù)
ncols = tab.ncols#列數(shù)
for x in range(0, nrows):
for y in range(0, ncols):
value = tab.cell(x,y).value
print(tab.cell(x, y).value)
def main():
# print('input the path of your file:')
# a = open_excel(r'D:\smt_ioe\untitled\analysis_excel\my.xls')
# print(a)
b = excel_by_index(r'D:\smt_ioe\untitled\analysis_excel\my.xls', 0, 2)
m = []
for i in range(b.__len__()):
c = b[i]
# a = c['name']
for x in c:
if x == 'date':
print(x)
print('meng')
read_excel(r'D:\smt_ioe\untitled\analysis_excel\my.xls',2)
if __name__ == '__main__':
main()感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“怎么使用python3+xlrd解析Excel的實(shí)例”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!
當(dāng)前標(biāo)題:怎么使用python3+xlrd解析Excel的實(shí)例-創(chuàng)新互聯(lián)
本文路徑:http://chinadenli.net/article36/poppg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計(jì)、用戶體驗(yàn)、網(wǎng)頁設(shè)計(jì)公司、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容