python讀取excel表

來源:互聯網
上載者:User

標籤:xrange   python   pytho   int   exce   work   from   read   一個   

from xlrd import open_workbook
import re

#建立一個用於讀取sheet的產生器,依次產生每行資料,row_count 用於指定讀取多少行, col_count 指定用於讀取多少列
def readsheet(s, row_count=-1, col_cout=-1):#
# Sheet 有多少行
nrows = s.nrows
# Sheet 有多少列
ncols = s.ncols
row_count = (row_count if row_count > 0 else nrows)
col_count = (col_count if col_count > 0 else ncols)
row_index = 0
while row_index < row_count:
yield [s.cell(row_index, col).value for col in xrange(col_count)]
row_index += 1

wb = open_workbook(‘simple.xls‘) #開啟Excel檔案
# 讀取Excel中所有的Sheet
for s in wb.sheets():
for row in readsheet(s, 10, 10):# 唯讀取每個Sheet的前10行,前10列(當然你要確保,你的資料多餘10行,且多餘10列)
print row

python讀取excel表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.