使用python3+xlrd解析Excel的實(shí)例
實(shí)例如下所示:
# -*- coding: utf-8 -*-
import xlrd
def open_excel(file = 'file.xls'):#打開(kāi)要解析的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)#打開(kāi)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)#打開(kāi)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()
以上這篇使用python3+xlrd解析Excel的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python實(shí)現(xiàn)經(jīng)典排序算法的示例代碼
這篇文章主要介紹了python實(shí)現(xiàn)經(jīng)典排序算法的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Python編程中非常重要卻又被嚴(yán)重低估的庫(kù)decorator
今天介紹的是一個(gè)已經(jīng)存在十年,但是依舊不紅的庫(kù) decorator,好像很少有人知道他的存在一樣。本篇文章不會(huì)過(guò)多的向你介紹裝飾器的基本知識(shí),我會(huì)默認(rèn)你知道什么是裝飾器,并且懂得如何寫(xiě)一個(gè)簡(jiǎn)單的裝飾器2021-10-10
Python 平方列表中每個(gè)數(shù)字的多種操作
這篇文章主要介紹了Python 平方列表中每個(gè)數(shù)字的多種操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03
如何將自己的python代碼發(fā)布在pip install給別人使用你知道嗎
這篇文章主要介紹了python如何發(fā)布自已的pip項(xiàng)目,方便大家學(xué)習(xí),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2021-08-08
python爬蟲(chóng)URL重試機(jī)制的實(shí)現(xiàn)方法(python2.7以及python3.5)
今天小編就為大家分享一篇python爬蟲(chóng)URL重試機(jī)制的實(shí)現(xiàn)方法(python2.7以及python3.5),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
python數(shù)組排序方法之sort、sorted和argsort詳解
這篇文章主要給大家介紹了關(guān)于python數(shù)組排序方法之sort、sorted和argsort的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03

