Python应用技巧:高效使用xlrd库读取Excel表数据
学习目录
了解下电脑中的excel表格文件格式
安装xlrd库
xlrd库读取表格内容
1 先准备一个表格‘表格.xls’,表格中包含两个sheet页
2 导入xlrd库
3 用一个图展示下xlrd常用的函数
4 分别展示下表格中按行/按列/按单元格获取的内容
5 拓展内容
excel表格是大家经常用到的文件格式,各行各业都会跟它打交道。本次我们介绍经常用到的两个经典库,xlrd和xlwt,xlrd用于读取excel表格内容,xlwt用于写入excel表格内容。
了解下电脑中的excel表格文件格式
微软或者金山的excel表格编辑保存时一般要选择文件后缀,有xls和xlsx两类。
xls和xlsx后缀文件的主要区别:
安装xlrd库
pip install xlrd -i https://mirrors.aliyun.com/pypi/simple/
xlrd库默认安装最新的库,新版本的xlrd只支持.xls文件,如果需要读取.xlsx文件时需要安装旧版本(比如指定版本xlrd==1.2.0)。
xlrd库读取表格内容
1 先准备一个表格‘表格.xls’,表格中包含两个sheet页。
2 导入xlrd库
执行import xlrd导入该库
3 用一个图展示下xlrd常用的函数
4 分别展示下表格中按行/按列/按单元格获取的内容
- 按行获取内容
#打开表格
data = xlrd.open_workbook('表格.xls')
#获取sheet1的内容
sheet1 = data.sheets()[0]
#行数
sheet1_nrows = sheet1.nrows
print(f'sheet1 行数:{sheet1_nrows}')
#结果
sheet1 行数:4
for i in range(sheet1_nrows):
sheet1_row_content = sheet1.row(i)
print(f'sheet1 第{i+1}行内容:{sheet1_row_content}')
#结果:
sheet1 第1行内容:[text:'user', text:'age']
sheet1 第2行内容:[text:'lili', number:21.0]
sheet1 第3行内容:[text:'zhangsan', number:13.0]
sheet1 第4行内容:[text:'lisi', number:35.0]
for i in range(sheet1_nrows):
sheet1_row_content = sheet1.row_values(i)
print(f'sheet1 第{i+1}行内容:{sheet1_row_content}')
#结果
sheet1 第1行内容:['user', 'age']
sheet1 第2行内容:['lili', 21.0]
sheet1 第3行内容:['zhangsan', 13.0]
sheet1 第4行内容:['lisi', 35.0]
for i in sheet1.get_rows():
print(f'sheet1 中的行内容:{i}')
#结果:
sheet1 中的行内容:[text:'user', text:'age']
sheet1 中的行内容:[text:'lili', number:21.0]
sheet1 中的行内容:[text:'zhangsan', number:13.0]
sheet1 中的行内容:[text:'lisi', number:35.0]
2 按列获取内容
sheet1_ncols = sheet1.ncols
print(f'sheet1 列数:{sheet1_ncols}')
for i in range(sheet1_ncols):
sheet1_col_content = sheet1.col(i)
print(f'sheet1 第{i+1}列内容:{sheet1_col_content}')
#结果:
sheet1 第1列内容:[text:'user', text:'lili', text:'zhangsan', text:'lisi']
sheet1 第2列内容:[text:'age', number:21.0, number:13.0, number:35.0]
for i in range(sheet1_ncols):
sheet1_col_content = sheet1.col_values(i)
print(f'sheet1 第{i+1}列内容:{sheet1_col_content}')
#结果
sheet1 第1列内容:['user', 'lili', 'zhangsan', 'lisi']
sheet1 第2列内容:['age', 21.0, 13.0, 35.0]
3 按单元格获取内容
print(f'sheet1 第1行 第2列内容:{sheet1.cell(0, 1).value}')
print(f'sheet1 第2行 第2列内容:{sheet1.cell_value(1, 1)}')
print(f'sheet1 第1行 第1列内容:{sheet1.row(0)[0].value}')
#结果:
sheet1 第1行 第2列内容:age
sheet1 第2行 第2列内容:21.0
sheet1 第1行 第1列内容:user
5 拓展内容
1)表格中数据类型介绍
0 – empty, 1 – text, 2 – number, 3 – date, 4 – boolean, 5 – error
表格sheet3中有如下内容:
获取数据类型如下:
sheet3 = data.sheets()[2]
row_type = sheet3.row_types(0)
print(row_type)
col_type = sheet3.col_types(1)
print(col_type)
cell_type = sheet3.cell_type(0,3)
print(cell_type)
#结果:
array('B', [0, 1, 2, 3, 4, 1])
[1]
3
2 获取行内容时发现填写的2023年10月8日返回的是数字
cell_content = sheet3.cell(0,3).value
print(cell_content)
#结果:
45207.0
原来excel表中的日期会被python根据一个时间基准计算出相差的的天数(1900-01-01和1904-01-01基准)
解决方法:使用
xlrd.xldate.xldate_as_datetime函数处理得到的数字,它会返回datetime对象,用strftime把它转化成指定格式的字符串。
print(xlrd.xldate.xldate_as_datetime(cell_content,0).strftime("%Y/%m/%d"))
结果:2023/10/08
—-感谢读者的阅读和学习,谢谢大家。
共勉: 东汉·班固《汉书·枚乘传》:“泰山之管穿石,单极之绠断干。水非石之钻,索非木之锯,渐靡使之然也。”
—–指水滴不断地滴,可以滴穿石头;
—–比喻坚持不懈,集细微的力量也能成就难能的功劳。
作者:科雷软件测试