python28 excel读取模块xlrd
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                python28 excel读取模块xlrd
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                安裝:
pip install xlrd?
簡單使用:
import xlrdbook = xlrd.open_workbook(r'C:\Users\dinghanhua\Desktop\yqqapi.xlsx') # 打開excel print("the number of sheets:",book.nsheets) # sheet數量 print("sheet_names:",book.sheet_names()) # sheetname列表 sheet1 = book.sheet_by_index(0) # 通過索引取sheet print(sheet1.name,sheet1.nrows,sheet1.ncols) #sheet名稱、行數、列數 print(sheet1.cell(0,0).value) #cell值 print(sheet1.cell_value(0,1)) #cell值 sheet2 = book.sheet_by_name("sheet2") # 通過sheetname取sheet print(sheet2.name,sheet2.nrows,sheet2.ncols)# 獲取sheet所有的數據 for row in range(sheet1.nrows):for col in range(sheet1.ncols):print(sheet1.cell_value(row,col),end='\t')print('\n')print(sheet1.col_values(0,1,sheet1.nrows)) # 獲取第一列,第2行的所有值print(sheet1.row(1)) # 獲取第二行的值for col in range(sheet1.ncols): # 按列獲取值,每列是listprint(sheet1.col_values(col,0,sheet1.nrows))for row in range(sheet1.nrows): # 按行獲取值;每行都是listprint(sheet1.row_values(row,0,sheet1.ncols))?
有的單元格帶有左右空格,取值時用strip()處理下
cell = str(sheet1.cell_value(0,0)).strip()?
?
the end!
?
轉載于:https://www.cnblogs.com/dinghanhua/p/10348076.html
總結
以上是生活随笔為你收集整理的python28 excel读取模块xlrd的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 浏览器窗口尺寸clientHeight
 - 下一篇: nginx 高并发优化参数