当前位置:网站首页>Xlrd common operations
Xlrd common operations
2022-07-05 21:23:00 【Lin Zai 520】
0. introduction
When I was working on the project in the early stage , Use of third parties python library openpyxl To read excel file , But when the amount of data is very large , Code execution time is very slow . Based on this , I refer to the relevant information , Discover the use of xlrd Can be greatly improved Python Read excel The efficiency of .
1. xlrd and xlwt Package installation
pip install xlrd
pip install xlwt
2. xlrd Package usage
# Guide pack
import xlrd
# Data files
data_file = '../ data / Basic data of track circuit 1.xlsx'
# The main function
if __name__ == "__main__":
''' 1. Open file '''
wb = xlrd.open_workbook(data_file)
print("==========================================================================")
''' 2. obtain sheet object '''
print("\n++++++++++++++++++++++++ 2. obtain sheet object ++++++++++++++++++++++++\n")
# 1. Get all sheet name
sheet_names = wb.sheet_names()
print("sheet_names:", sheet_names)
# 2. obtain sheet Number
sheet_number = wb.nsheets
print("sheet_number:", sheet_names)
# 3. Get all sheet object
sheet_objct = wb.sheets()
print(sheet_objct)
# 4. adopt sheet Name search
sheet_by_name = wb.sheet_by_name(" Basic data ")
print("sheet_by_name:", sheet_by_name)
# 5. Search by index ( from 0 Start )
sheet_by_index = wb.sheet_by_index(0)
print("sheet_by_index:", sheet_by_index)
print("==========================================================================")
''' 3. obtain sheet The aggregate data of '''
print("\n++++++++++++++++++++++++ 3. obtain sheet The aggregate data of ++++++++++++++++++++++++\n")
sheet = wb.sheet_by_index(0)
# 1. obtain sheet name
sheet_name = sheet.name
print("sheet_name:", sheet_name)
# 2. obtain sheet The total number of columns
num_cols = sheet.ncols
print("num_cols:", num_cols)
# 3. obtain sheet Total number of lines
num_rows = sheet.nrows
print("num_rows:", num_rows)
print("==========================================================================")
''' 4. Cell batch reading '''
# a. Line operation
print("\n++++++++++++++++++++++++ 4. Cell batch reading —— Line operation ++++++++++++++++++++++++\n")
# 1. Get everything in the first line
row_1_values = sheet.row_values(0)
print(" All contents in the first line :",row_1_values)
# 2. Get cell value type and content
row_1 = sheet.row(0)
print(" Cell value type and content :",row_1)
# 3. Get cell data type
row_1_type = sheet.row_types(0)
print(" Get cell data type :",row_1_type)
print("==========================================================================")
# b. Table operations
print("\n++++++++++++++++++++++++ 4. Cell batch reading —— Table operations ++++++++++++++++++++++++\n")
# 1. Take the first place 1 That's ok , The first 6~10 Column
result1 = sheet.row_values(0, 6, 10)
print(" The first 1 That's ok , The first 6~10 Column :", result1)
# 2. Take the first column , The first 0~5 That's ok
result2 = sheet.col_values(0, 0, 5)
print(" First column , The first 0~5 That's ok :", result2)
# 3. Get cell value type and content
result3 = sheet.row_slice(2, 0, 2)
print(" Cell value type and content :", result3)
# 4. Get cell data type
result4 = sheet.row_types(1, 0, 2)
print(" Cell data type :", result4)
print("==========================================================================")
''' 5. Specific cell read '''
# a. Get cell values
print("\n++++++++++++++++++++++++ 5. Specific cell read —— Get cell values ++++++++++++++++++++++++\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. Get cell type
print("\n++++++++++++++++++++++++ 5. Specific cell read —— Get cell type ++++++++++++++++++++++++\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. Some common transformations '''
print("\n++++++++++++++++++++++++ 6. Some common transformations ++++++++++++++++++++++++\n")
# (0,0) convert to A1
result1 = xlrd.cellname(0,0)
print("(0,0) convert to A1:", result1)
# (0,0) convert to $A$1
result2 = xlrd.cellnameabs(0,0)
print("(0,0) convert to $A$1:", result2)
# Convert columns from numbers to letters
result3 = xlrd.colname(40)
print(" Convert columns from numbers to letters :xlrd.colname(40): ", result3)
2. Program run results
边栏推荐
- Problems encountered in office--
- Clion-MinGW编译后的exe文件添加ico图标
- 【案例】定位的运用-淘宝轮播图
- Comprehensive optimization of event R & D workflow | Erda version 2.2 comes as "7"
- How to send samples when applying for BS 476-7 display? Is it the same as the display??
- Learning robots have no way to start? Let me show you the current hot research directions of robots
- Dictionary tree simple introductory question (actually blue question?)
- Some things make feelings nowhere to put
- POJ 3414 pots (bfs+ clues)
- Determine the best implementation of horizontal and vertical screens
猜你喜欢
Pytorch实战——MNIST数据集手写数字识别
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
使用WebAssembly在浏览器端操作Excel
MQ----activeMq
面试官:并发编程实战会吗?(线程控制操作详解)
Why can't Chinese software companies produce products? Abandon the Internet after 00; Open source high-performance API gateway component of station B | weekly email exclusive to VIP members of Menon w
Add ICO icon to clion MinGW compiled EXE file
Five layer network protocol
Teach yourself to train pytorch model to Caffe (2)
基於flask寫一個接口
随机推荐
The primary key is set after the table is created, but auto increment is not set
Écrire une interface basée sur flask
第05章_存储引擎
EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
让开发效率飞速提升的跨端方案
Influence of oscilloscope probe on signal source impedance
【日常训练--腾讯精选50】89. 格雷编码(看题解才会的)
Talk about my fate with some programming languages
Reading and writing operations of easyexcel
When a user logs in, there is often a real-time drop-down box. For example, entering an email will @qq com,@163. com,@sohu. com
Mode - "Richter replacement principle"
Is Kai Niu 2980 useful? Is it safe to open an account
Five layer network protocol
Aitm2-0002 12s or 60s vertical combustion test
基於flask寫一個接口
Problems encountered in office--
Using webassembly to operate excel on the browser side
Matplotlib drawing retouching (how to form high-quality drawings, such as how to set fonts, etc.)
int GetMonth( ) const throw( );后面的throw( )什么意思?
2022-07-03-CKA-粉丝反馈最新情况