当前位置:网站首页>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. 程序运行结果
边栏推荐
- 【日常训练】729. 我的日程安排表 I
- selenium 查找b或p标签的内容
- Learning notes of SAS programming and data mining business case 19
- Test of incombustibility of cement adhesives BS 476-4
- Écrire une interface basée sur flask
- 【案例】元素的显示与隐藏的运用--元素遮罩
- 基于flask写一个接口
- Dictionary tree simple introductory question (actually blue question?)
- Reading and writing operations of easyexcel
- EasyExcel的讀寫操作
猜你喜欢
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
Pytoch practice -- MNIST dataset handwritten digit recognition
Reading and writing operations of easyexcel
EN 438-7 laminated sheet products for building covering decoration - CE certification
Access Zadig self-test environment outside the cluster based on ingress controller (best practice)
Golang (1) | from environmental preparation to quick start
【案例】定位的运用-淘宝轮播图
浅聊我和一些编程语言的缘分
2022-07-03-cka- latest feedback from fans
leetcode:1139. The largest square bounded by 1
随机推荐
树莓派4B上ncnn转换出来的模型调用时总是崩溃(Segment Fault)的原因
int GetMonth( ) const throw( ); What does throw () mean?
Cross end solution to improve development efficiency rapidly
CLion配置visual studio(msvc)和JOM多核编译
SQL series (basic) - Chapter 2 limiting and sorting data
Postgres establish connection and delete records
Influence of oscilloscope probe on measurement bandwidth
Pytoch practice -- MNIST dataset handwritten digit recognition
Traps in the explode function in PHP
使用WebAssembly在浏览器端操作Excel
Get JS of the previous day (timestamp conversion)
Opérations de lecture et d'écriture pour easyexcel
PostGIS installation geographic information extension
事项研发工作流全面优化|Erda 2.2 版本如“七”而至
@Validated基础参数校验、分组参数验证和嵌套参数验证
php中explode函数存在的陷阱
Which securities company is better and which platform is safer for stock account opening
Deep merge object deep copy of vant source code parsing
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
EN 438-7 laminated sheet products for building covering decoration - CE certification