python如何读取excel表数据
发布时间:2020年10月29日 09:26:54
来源:环球青藤
点击量:348
【摘要】python读取excel表数据的方法:首先安装Excel读取数据的库xlrd;然后获取Excel文件的位置并且读取进来;接着读取指定的行和列的内容,并将
python读取excel表数据的方法:首先安装Excel读取数据的库xlrd;然后获取Excel文件的位置并且读取进来;接着读取指定的行和列的内容,并将内容存储在列表中;最后运行程序即可。
python读取excel表数据的方法:
1、安装Excel读取数据的库-----xlrd
直接pip install xlrd安装xlrd库
#引入Excel库的xlrd
import xlrd
2、获取Excel文件的位置并且读取进来
#导入需要读取Excel表格的路径
data = xlrd.open_workbook(r'C:UsersNHTDesktopDatatest1.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':'','timepne':'','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['timepne'] = 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:UsersNHTDesktopDatatest.xlsx')
table = data1.sheets()[0]
#创建一个空列表,存储Excel的数据
tables = []
#将excel表格内容导入到tables列表中
def import_excel(excel):
for rown in range(excel.nrows):
array = {'road_name':'','bus_plate':'','timepne':'','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['timepne'] = 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视频教程
以上就是小编分享的关于python如何读取excel表数据的详细内容希望对大家有所帮助,更多有关python教程请关注环球青藤其它相关文章!
上一篇:怎么查看python版本?
下一篇:学习Python模块导入机制与大型项目的规范
就业培训申请领取
环球青藤
官方QQ群扫描上方二维码或点击一键加群,免费领取大礼包,加群暗号:青藤。 一键加群
最新文章
Python编程各地入口
环球青藤官方微信服务平台
刷题看课 APP下载
免费直播 一键购课
代报名等人工服务
Python编程热点排行