我是靠谱客的博主 朴实季节,这篇文章主要介绍python如何读取excel表数据,现在分享给大家,希望可以做个参考。

python读取excel表数据的方法:

1、安装Excel读取数据的库-----xlrd

直接pip install xlrd安装xlrd库

复制代码
1
2
#引入Excel库的xlrd import xlrd
登录后复制

2、获取Excel文件的位置并且读取进来

复制代码
1
2
3
#导入需要读取Excel表格的路径 data = xlrd.open_workbook(r'C:UsersNHTDesktopData\test1.xlsx') table = data.sheets()[0]
登录后复制

3、读取指定的行和列的内容,并将内容存储在列表中(将第三列的时间格式转换)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#创建一个空列表,存储Excel的数据 tables = [] #将excel表格内容导入到tables列表中 def import_excel(excel): for rown in range(excel.nrows): array = {'road_name':'','bus_plate':'','timeline':'','road_type':'','site':''} array['road_name'] = table.cell_value(rown,0) array['bus_plate'] = table.cell_value(rown,1) #将Excel表格中的时间格式转化 if table.cell(rown,2).ctype == 3: date = xldate_as_tuple(table.cell(rown,2).value,0) array['timeline'] = datetime.datetime(*date) array['road_type'] = table.cell_value(rown,3) array['site'] = table.cell_value(rown,4) tables.append(array)
登录后复制

4、运行程序

复制代码
1
2
3
4
5
6
if __name__ == '__main__': #将excel表格的内容导入到列表中 import_excel(table) #验证Excel文件存储到列表中的数据 for i in tables: print(i)
登录后复制

5、最终的运行效果如下:

99ff66bc4315a7e099339409ffa5d80.png

6、完整的程序代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import xlrd from xlrd import xldate_as_tuple import datetime #导入需要读取的第一个Excel表格的路径 data1 = xlrd.open_workbook(r'C:UsersNHTDesktopData\test.xlsx') table = data1.sheets()[0] #创建一个空列表,存储Excel的数据 tables = [] #将excel表格内容导入到tables列表中 def import_excel(excel): for rown in range(excel.nrows): array = {'road_name':'','bus_plate':'','timeline':'','road_type':'','site':''} array['road_name'] = table.cell_value(rown,0) array['bus_plate'] = table.cell_value(rown,1) if table.cell(rown,2).ctype == 3: date = xldate_as_tuple(table.cell(rown,2).value,0) array['timeline'] = datetime.datetime(*date) array['road_type'] = table.cell_value(rown,3) array['site'] = table.cell_value(rown,4) tables.append(array) if __name__ == '__main__': #将excel表格的内容导入到列表中 import_excel(table) for i in tables: print(i)
登录后复制

以上就是python如何读取excel表数据的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是朴实季节最近收集整理的关于python如何读取excel表数据的全部内容,更多相关python如何读取excel表数据内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部