python之讀取Excel 檔案

來源:互聯網
上載者:User

標籤:ble   style   基本   override   mod   rbo   log   data   api   

 1 # -*- coding: utf-8 -*- 2 """ 3 Created on Thu May 24 13:53:10 2018 4  5 @author: Frank 6 """ 7  8 import xlrd  #xlrd is a library for reading data and formatting information from Excel files, whether they are .xls or .xlsx files. 9 10 data = xlrd.open_workbook(‘通訊錄.xlsx‘)#開啟 Excel檔案11 print(type(data))12 table = data.sheets()[0]13 print(type(table))14 nrows = table.nrows #行數15 print("行數:{}".format(nrows))16 ncols = table.ncols #列數17 print("列數:{}".format(ncols))18 for i in range(0,nrows):19     rowValues = table.row_values(i) #某一行資料 20     #print(type(rowValues))21     #print(rowValues)22     print("姓名:{}<-->電話:{}".format(rowValues[0], rowValues[1]))

 

xlrd.open_workbook(filename=Nonelogfile=<_io.TextIOWrapper name=‘<stdout>‘ mode=‘w‘ encoding=‘UTF-8‘>verbosity=0use_mmap=1file_contents=Noneencoding_override=Noneformatting_info=Falseon_demand=Falseragged_rows=False)

開啟一個Excel檔案。

這裡先簡單的介紹基本的參數,以後有用到再添加。

Parameters:

  filename – The path to the spreadsheet file to be opened.

    指定要開啟檔案的路徑

  logfile – An open file to which messages and diagnostics are written  

Returns:

  An instance of the Book class.

 

book.sheets()

Return:  A list of all sheets in the book. 返回Excel中所有的表,並儲存在list中。

  All sheets not already loaded will be loaded.

 

classxlrd.sheet.Sheet(bookpositionnamenumber)

 

Contains the data for one worksheet.

In the cell access functions, rowx is a row index, counting from zero, and colx is a column index, counting from zero. Negative values for row/column indexes and slice positions are supported in the expected fashion.

Note: You don’t instantiate this class yourself. You access Sheet objects via the Book object that was returned when you called xlrd.open_workbook().

nrows= 0

  Number of rows in sheet. A row index is in range(thesheet.nrows).

ncols= 0

  Nominal number of columns in sheet. It is one more than the maximum column index found, ignoring trailing empty cells. See also the ragged_rows parameter to open_workbook() and row_len().

row_values(rowxstart_colx=0end_colx=None)

  Returns a slice of the values of the cells in the given row.該函數返回一個list,預設包含這一行的cells的所有內容。

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.