当前位置:网站首页>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

边栏推荐
- leetcode:1139. The largest square bounded by 1
- R language [data management]
- 張麗俊:穿透不確定性要靠四個“不變”
- 字典树简单入门题(居然是蓝题?)
- Golang(1)|从环境准备到快速上手
- Talk about my fate with some programming languages
- 大二下个人发展小结
- The primary key is set after the table is created, but auto increment is not set
- [case] Application of positioning - Taobao rotation map
- 【日常训练】729. 我的日程安排表 I
猜你喜欢

EasyExcel的讀寫操作

Pytorch实战——MNIST数据集手写数字识别

Deployment of Jenkins under win7

事项研发工作流全面优化|Erda 2.2 版本如“七”而至

2022-07-03-CKA-粉丝反馈最新情况

示波器探头对信号源阻抗的影响

MySQL InnoDB Architecture Principle

MySQL deep paging optimization with tens of millions of data, and online failure is rejected!

Pytoch practice -- MNIST dataset handwritten digit recognition

CLion配置visual studio(msvc)和JOM多核编译
随机推荐
@Validated basic parameter verification, grouping parameter verification and nested parameter verification
@Validated基础参数校验、分组参数验证和嵌套参数验证
Utils/index TS tool function
Uni app Bluetooth communication
Talk about my fate with some programming languages
有些事情让感情无处安放
Wood board ISO 5660-1 heat release rate mapping test
Modifiers of attributes of TS public, private, protect
Display DIN 4102-1 Class B1 fire test requirements
字典树简单入门题(居然是蓝题?)
張麗俊:穿透不確定性要靠四個“不變”
LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
Simple interest mode - evil Chinese style
Enclosed please find. Net Maui's latest learning resources
Objects in the list, sorted by a field
Selenium's method of getting attribute values in DOM
Zhang Lijun: la pénétration de l’incertitude dépend de quatre « invariants»
EN 438-7 laminated sheet products for building covering decoration - CE certification
力扣------经营摩天轮的最大利润
SYSTEMd resolved enable debug log