当前位置:网站首页>xlrd常见操作
xlrd常见操作
2022-07-05 21:20:00 【林仔520】
0. 导言
笔者前期在做项目时,使用第三方python库openpyxl来读取excel文件,但是当数据量非常大时,代码执行时间非常缓慢。基于此,笔者查阅相关资料,发现使用xlrd能大幅度提升Python读取excel的效率。
1. xlrd和xlwt包安装
pip install xlrd
pip install xlwt
2. xlrd包使用
# 导包
import xlrd
# 数据文件
data_file = '../数据/轨道电路基础数据1.xlsx'
# 主函数
if __name__ == "__main__":
''' 1. 打开文件 '''
wb = xlrd.open_workbook(data_file)
print("==========================================================================")
''' 2. 获取sheet对象 '''
print("\n++++++++++++++++++++++++ 2. 获取sheet对象 ++++++++++++++++++++++++\n")
# 1. 获取所有sheet名字
sheet_names = wb.sheet_names()
print("sheet_names:", sheet_names)
# 2. 获取sheet数量
sheet_number = wb.nsheets
print("sheet_number:", sheet_names)
# 3. 获取所有sheet对象
sheet_objct = wb.sheets()
print(sheet_objct)
# 4. 通过sheet名查找
sheet_by_name = wb.sheet_by_name("基础数据")
print("sheet_by_name:", sheet_by_name)
# 5. 通过索引查找(从0开始)
sheet_by_index = wb.sheet_by_index(0)
print("sheet_by_index:", sheet_by_index)
print("==========================================================================")
''' 3. 获取sheet的汇总数据 '''
print("\n++++++++++++++++++++++++ 3. 获取sheet的汇总数据 ++++++++++++++++++++++++\n")
sheet = wb.sheet_by_index(0)
# 1. 获取sheet名
sheet_name = sheet.name
print("sheet_name:", sheet_name)
# 2. 获取sheet总列数
num_cols = sheet.ncols
print("num_cols:", num_cols)
# 3. 获取sheet总行数
num_rows = sheet.nrows
print("num_rows:", num_rows)
print("==========================================================================")
''' 4. 单元格批量读取 '''
# a.行操作
print("\n++++++++++++++++++++++++ 4. 单元格批量读取 —— 行操作 ++++++++++++++++++++++++\n")
# 1. 获取第一行所有内容
row_1_values = sheet.row_values(0)
print("第一行所有内容:",row_1_values)
# 2. 获取单元格值类型和内容
row_1 = sheet.row(0)
print("单元格值类型和内容:",row_1)
# 3. 获取单元格数据类型
row_1_type = sheet.row_types(0)
print("获取单元格数据类型:",row_1_type)
print("==========================================================================")
# b. 表操作
print("\n++++++++++++++++++++++++ 4. 单元格批量读取 —— 表操作 ++++++++++++++++++++++++\n")
# 1. 取第1行,第6~10列
result1 = sheet.row_values(0, 6, 10)
print("第1行,第6~10列:", result1)
# 2. 取第一列,第0~5行
result2 = sheet.col_values(0, 0, 5)
print("第一列,第0~5行:", result2)
# 3. 获取单元格值类型和内容
result3 = sheet.row_slice(2, 0, 2)
print("单元格值类型和内容:", result3)
# 4. 获取单元格数据类型
result4 = sheet.row_types(1, 0, 2)
print("单元格数据类型:", result4)
print("==========================================================================")
''' 5. 特定单元格读取 '''
# a.获取单元格值
print("\n++++++++++++++++++++++++ 5. 特定单元格读取 —— 获取单元格值 ++++++++++++++++++++++++\n")
result1 = sheet.cell_value(1, 2)
print("sheet.cell_value(1, 2):", result1)
result2 = sheet.cell(1,2).value
print("sheet.cell(1,2).value:", result2)
result3 = sheet.row(1)[2].value
print("sheet.row(1)[2].value:", result3)
print("==========================================================================")
# b.获取单元格类型
print("\n++++++++++++++++++++++++ 5. 特定单元格读取 —— 获取单元格类型 ++++++++++++++++++++++++\n")
result1 = sheet.cell(1,2).ctype
print("sheet.cell(1,2).ctype:", result1)
result2 = sheet.cell_type(1,2)
print("sheet.cell_type(1,2):", result2)
result3 = sheet.row(1)[2].ctype
print("sheet.row(1)[2].ctype:", result3)
''' 6. 常见的一些转换 '''
print("\n++++++++++++++++++++++++ 6. 常见的一些转换 ++++++++++++++++++++++++\n")
# (0,0)转换成A1
result1 = xlrd.cellname(0,0)
print("(0,0)转换成A1:", result1)
# (0,0)转换成$A$1
result2 = xlrd.cellnameabs(0,0)
print("(0,0)转换成$A$1:", result2)
# 把列由数字转换为字母表示
result3 = xlrd.colname(40)
print("把列由数字转换为字母表示:xlrd.colname(40): ", result3)
2. 程序运行结果

边栏推荐
- The reason why the ncnn converted model on raspberry pie 4B always crashes when called
- Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
- 五层网络协议
- Is Kai Niu 2980 useful? Is it safe to open an account
- 123456
- Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
- Aitm 2-0003 horizontal combustion test
- vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
- php中explode函数存在的陷阱
- JS common method encapsulation
猜你喜欢

浅聊我和一些编程语言的缘分

The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application

Two ways to realize video recording based on avfoundation

Talk about my fate with some programming languages

请查收.NET MAUI 的最新学习资源

XML modeling

Introduction of ArcGIS grid resampling method

Realize the function of verifying whether the user has completed login when browsing the page

第05章_存储引擎

Parker驱动器维修COMPAX控制器维修CPX0200H
随机推荐
Enclosed please find. Net Maui's latest learning resources
Cross end solution to improve development efficiency rapidly
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
Selenium's method of getting attribute values in DOM
Learning robots have no way to start? Let me show you the current hot research directions of robots
Influence of oscilloscope probe on signal source impedance
EasyExcel的讀寫操作
Clion configures Visual Studio (MSVC) and JOM multi-core compilation
Aitm 2-0003 horizontal combustion test
Influence of oscilloscope probe on measurement bandwidth
张丽俊:穿透不确定性要靠四个“不变”
《SAS编程和数据挖掘商业案例》学习笔记# 19
Écrire une interface basée sur flask
【案例】元素的显示与隐藏的运用--元素遮罩
Postgres establish connection and delete records
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
Write an interface based on flask
Clickhouse copy paste multi line SQL statement error
显示屏DIN 4102-1 Class B1防火测试要求
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session