python读取Excel表格文件的方法
作者:迷途&羔羊 时间:2023-07-24 01:17:14
python读取Excel表格文件,例如获取这个文件的数据
python读取Excel表格文件,需要如下步骤:
1、安装Excel读取数据的库-----xlrd
直接pip install xlrd安装xlrd库
#引入Excel库的xlrd
import xlrd
2、获取Excel文件的位置并且读取进来
#导入需要读取Excel表格的路径
data = xlrd.open_workbook(r'C:\Users\NHT\Desktop\Data\\test1.xlsx')
table = data.sheets()[0]
3、读取指定的行和列的内容,并将内容存储在列表中(将第三列的时间格式转换)
#创建一个空列表,存储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、运行程序
if __name__ == '__main__':
#将excel表格的内容导入到列表中
import_excel(table)
#验证Excel文件存储到列表中的数据
for i in tables:
print(i)
5、最终的运行效果如下:
6、完整的程序代码:
import xlrd
from xlrd import xldate_as_tuple
import datetime
#导入需要读取的第一个Excel表格的路径
data1 = xlrd.open_workbook(r'C:\Users\NHT\Desktop\Data\\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表格文件的方法,网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
来源:https://blog.csdn.net/weixin_39082390/article/details/97373951
标签:python,读取,Excel,表格,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现智能贪吃蛇游戏的示例代码
2023-01-28 13:39:36
![](https://img.aspxhome.com/file/2023/1/100861_0s.jpg)
node.js express安装及示例网站搭建方法(分享)
2024-05-13 09:58:22
![](https://img.aspxhome.com/file/2023/4/125854_0s.png)
Python编程求质数实例代码
2021-12-03 23:17:18
![](https://img.aspxhome.com/file/2023/3/117993_0s.png)
golang语言实现的文件上传与文件下载功能示例
2023-06-19 00:05:31
SQLServer Execpt和not in 性能区别
2024-01-21 23:33:46
浅析Python中的随机采样和概率分布
2023-12-11 14:51:11
管理员必读10个重要MySQL客户启动选项
2008-06-07 16:57:00
![](https://img.aspxhome.com/file/UploadPic/20086/7/200867171031126s.jpg)
Python实现检测文件的MD5值来查找重复文件案例
2021-08-14 01:28:02
浅谈Go语言并发机制
2024-05-10 13:58:38
![](https://img.aspxhome.com/file/2023/6/127476_0s.jpg)
uniapp H5 https跨域请求实现
2024-04-10 16:20:27
![](https://img.aspxhome.com/file/2023/0/136700_0s.png)
解决golang读取http的body时遇到的坑
2024-02-13 19:47:32
Python中dataclass库实例详解
2023-11-30 02:14:35
M2实现Nodejs项目自动部署的方法步骤
2024-05-13 10:05:21
php cli换行示例
2024-05-03 15:50:59
python socket 聊天室实例代码详解
2023-06-03 16:37:57
golang类型转换组件Cast的使用详解
2024-05-08 10:22:01
如何有效防止sql注入的方法
2024-01-18 21:18:13
![](https://img.aspxhome.com/file/2023/4/116324_0s.png)
MySql常用操作SQL语句汇总
2024-01-21 00:11:53
Python图片验证码降噪和8邻域降噪
2023-09-16 10:05:49
![](https://img.aspxhome.com/file/2023/2/95562_0s.bmp)
用js+cookie记录滚动条位置
2024-06-05 09:11:02