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

边栏推荐
- Realize the function of verifying whether the user has completed login when browsing the page
- Problems encountered in office--
- xlrd常见操作
- Sophomore personal development summary
- 终端安全能力验证环境搭建和渗透测试记录
- EN 438-7建筑覆盖物装饰用层压板材产品—CE认证
- Teach yourself to train pytorch model to Caffe (III)
- Careercup its 1.8 serial shift includes problems
- 示波器探头对测量带宽的影响
- Talk about my fate with some programming languages
猜你喜欢

Deployment of Jenkins under win7
![Longest swing sequence [greedy practice]](/img/e1/70dc21b924232c7e5e3da023a4bed2.png)
Longest swing sequence [greedy practice]

Clickhouse copy paste multi line SQL statement error

ArcGIS栅格重采样方法介绍

秋招将临 如何准备算法面试、回答算法面试题

校招期间 准备面试算法岗位 该怎么做?

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

leetcode:1755. Sum of subsequences closest to the target value

Clion-MinGW编译后的exe文件添加ico图标

ArcGIS\QGIS无插件加载(无偏移)MapBox高清影像图
随机推荐
R language [data management]
Two ways to realize video recording based on avfoundation
Test of incombustibility of cement adhesives BS 476-4
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
字典树简单入门题(居然是蓝题?)
让开发效率飞速提升的跨端方案
EN 438-7 laminated sheet products for building covering decoration - CE certification
Pytorch实战——MNIST数据集手写数字识别
浅聊我和一些编程语言的缘分
五层网络协议
示波器探头对测量带宽的影响
Deployment of Jenkins under win7
js常用方法封装
Arcgis\qgis no plug-in loading (no offset) mapbox HD image map
JMeter installation under win7
php中explode函数存在的陷阱
Enclosed please find. Net Maui's latest learning resources
How to prepare for the algorithm interview and answer the algorithm interview questions
使用WebAssembly在浏览器端操作Excel
Generics of TS