python28 excel读取模块xlrd
生活随笔
收集整理的這篇文章主要介紹了
python28 excel读取模块xlrd
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
安裝:
pip install xlrd?
簡(jiǎn)單使用:
import xlrdbook = xlrd.open_workbook(r'C:\Users\dinghanhua\Desktop\yqqapi.xlsx') # 打開(kāi)excel print("the number of sheets:",book.nsheets) # sheet數(shù)量 print("sheet_names:",book.sheet_names()) # sheetname列表 sheet1 = book.sheet_by_index(0) # 通過(guò)索引取sheet print(sheet1.name,sheet1.nrows,sheet1.ncols) #sheet名稱(chēng)、行數(shù)、列數(shù) print(sheet1.cell(0,0).value) #cell值 print(sheet1.cell_value(0,1)) #cell值 sheet2 = book.sheet_by_name("sheet2") # 通過(guò)sheetname取sheet print(sheet2.name,sheet2.nrows,sheet2.ncols)# 獲取sheet所有的數(shù)據(jù) 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))?
有的單元格帶有左右空格,取值時(shí)用strip()處理下
cell = str(sheet1.cell_value(0,0)).strip()?
?
the end!
?
轉(zhuǎn)載于:https://www.cnblogs.com/dinghanhua/p/10348076.html
總結(jié)
以上是生活随笔為你收集整理的python28 excel读取模块xlrd的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 浏览器窗口尺寸clientHeight
- 下一篇: websocket python爬虫_p