天天看点

python 读取excel表数据_Python读取excel表

python 读取excel表数据_Python读取excel表

  在我们做平常工作或自动化测试中都会遇到操作excel,Python处理exc相当顺手,如何通过python操作excel,当然python操作excel的库有很多,比如pandas,xlwt/xlrd,openpyxl等,每个库都有不同的区别,具体的区别,大家一起研究下哈。今天先讲xlrd来读取excl

xlrd模块

xlrd是对于Excel进行读取,xlrd 操作的是xls/xlxs格式的excel

安装

xlrd是python的第3方库,需要通过pip进行安装,即,pip install xlrd

python 读取excel表数据_Python读取excel表
python 读取excel表数据_Python读取excel表

1.在excel中存放数据,第一行为标题,也就是对应字典里面的key值,如:num,name

2.如果excel数据中有纯数字的一定要右键》设置单元格格式》文本格式,要不然读取的数据是浮点数

(先设置单元格格式后再编辑,编辑成功左上角有个小三角图标)

1.打开execl文件并获得所有sheet

实例:

import

xlrd

data

=

xlrd.open_workbook

('F:/test/student.xlsx')

# 运行结果:[u'name1', u'name2']

print(

data.sheet_names

())

2.根据下标获得sheet名称

实例:

import

xlrd

data

=

xlrd.open_workbook

('F:/test/student.xlsx')

# 运行结果:name2

print(

data.sheet_names

()[

1

])

3.获取行数和例数

实例:

python 读取excel表数据_Python读取excel表

4.获得某一行的值或某一列的值

实例:

python 读取excel表数据_Python读取excel表

5.获取指定单元格的内容和类型

实例:

import

xlrd

data

=

xlrd.open_workbook

('F:/test/student.xlsx')

# 通过名称获得name2对像

table_index

=

data.sheet_by_name

("name1")

# 括号内参数为:第2行,第2列

# 运行结果:text:u'Python'

print

table_index.cell

(

1, 1

)

# 获取单元格内容的类型

# 运行结果:1

print

table_index.cell

(

1, 0

)

.ctype

注:类型说明:ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error 更多用法请自行查询

软件测试、接口测试、自动化测试、面试经验交流。感兴趣可以关注我们码上开始,公众号内会有不定期的发放免费的资料链接,还有同行一起技术交流。这些资料都是从各个技术网站搜集、整理出来的,如果你有好的学习资料可以私聊发我,我会注明出处之后分享给大家。