網(wǎng)站首頁 編程語言 正文
一、基礎(chǔ)、常用方法
1. 讀取excel
1、導(dǎo)入模塊:
import xlrd
2、打開文件:
x1 = xlrd.open_workbook("data.xlsx")
3、獲取sheet:
sheet是指工作表的名稱,因為一個excel有多個工作表
獲取所有sheet名字:x1.sheet_names()
獲取sheet數(shù)量:x1.nsheets
獲取所有sheet對象:x1.sheets()
通過sheet名查找:x1.sheet_by_name("test”)
通過索引查找:x1.sheet_by_index(3)
# -*- coding:utf-8 -*- import xlrd import os filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename) print filePath # 1、打開文件 x1 = xlrd.open_workbook(filePath) # 2、獲取sheet對象 print 'sheet_names:', x1.sheet_names() # 獲取所有sheet名字 print 'sheet_number:', x1.nsheets # 獲取sheet數(shù)量 print 'sheet_object:', x1.sheets() # 獲取所有sheet對象 print 'By_name:', x1.sheet_by_name("test") # 通過sheet名查找 print 'By_index:', x1.sheet_by_index(3) # 通過索引查找
輸出:
sheet_names: [u' plan', u'team building', u'modile', u'test'] sheet_number: 4 sheet_object: [, , , ] By_name: By_index:
4、獲取sheet的匯總數(shù)據(jù):
獲取sheet名:sheet1.name
獲取總行數(shù):sheet1.nrows
獲取總列數(shù):sheet1.ncols
# -*- coding:utf-8 -*- import xlrd import os from datetime import date,datetime filename = "demo.xlsx" filePath = os.path.join(os.getcwd(), filename) print filePath # 打開文件 x1 = xlrd.open_workbook(filePath) # 獲取sheet的匯總數(shù)據(jù) sheet1 = x1.sheet_by_name("plan") print "sheet name:", sheet1.name # get sheet name print "row num:", sheet1.nrows # get sheet all rows number print "col num:", sheet1.ncols # get sheet all columns number
輸出:
sheet name: plan
row num: 31
col num: 11
資料:https://www.jb51.net/article/239873.htm
https://www.jb51.net/article/187025.htm
二、提高
三、出錯
1.無法打開.xlsx文件 pandas無法打開.xlsx文件,xlrd.biffh.XLRDError: Excel xlsx file; not supported
安裝的版本太高,低版本支持
可以安裝舊版xlrd,在cmd中運行:
pip uninstall xlrd pip install xlrd==1.2.0
也可以用openpyxl代替xlrd打開.xlsx文件:
df=pandas.read_excel(‘data.xlsx',engine=‘openpyxl')
總結(jié)
原文鏈接:https://blog.csdn.net/sinat_37281674/article/details/123293158
相關(guān)推薦
- 2021-12-26 WebStorm?發(fā)布2021.3重大更新新功能介紹_其它綜合
- 2022-08-26 Jquery實現(xiàn)下拉菜單案例_jquery
- 2023-10-12 ant-design的Input輸入框取消選擇后的藍(lán)色背景以及如何取消提示
- 2022-05-20 springCloud_Nacos服務(wù)搭建
- 2022-01-17 將字符串轉(zhuǎn)換成時間戳,yyyymmss到y(tǒng)yyy-mm-dd ,之后從時間戳轉(zhuǎn)換成時間格式字符串
- 2022-04-21 Python的索引與切片原來該這樣理解_python
- 2022-07-22 spring-boot設(shè)置跨域訪問方式
- 2022-04-25 Pytorch技法之繼承Subset類完成自定義數(shù)據(jù)拆分_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支