您现在的位置是:群英 > 开发技术 > Python语言
Python读取excel表的方法是什么?一文带你看懂
Admin发表于 2022-02-19 18:02:261374 次浏览

    Python读取excel表的方法是什么?首先,我们需要先安装Excel读取数据的库,然后再对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表的方法介绍啦,希望大家阅读完这篇文章能有所收获。最后,想要了解更多Python写入读取excel表的操作,大家可以关注群英网络其它相关文章。

文本转载自PHP中文网

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。

相关信息推荐
2022-06-28 17:05:00 
摘要:我一直在研究 Go,具体的说应该是一直在研究 gin ,我的 pet 项目需要一个快速而粗糙的身份认证, 通常我不会建议使用 HTTP / basic auth 来保护资源。
2022-08-20 17:56:12 
摘要:理解关联规则apriori算法:Apriori算法是第一个关联规则挖掘算法,也是最经典的算法,它利用逐层搜索的迭代方法找出数据库中项集的关系,以形成规则,其过程由连接【类矩阵运算】与剪枝【去掉那些没必要的中间结果】组成。
2021-12-08 18:10:52 
摘要:这篇文章主要介绍python中序列操作的相关内容,对大家了解和学习python中序列操作函数及其使用有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章能有所收获,接下来小编带着大家一起了解看看。
云活动
推荐内容
热门关键词
热门信息
群英网络助力开启安全的云计算之旅
立即注册,领取新人大礼包
  • 联系我们
  • 24小时售后:4006784567
  • 24小时TEL :0668-2555666
  • 售前咨询TEL:400-678-4567

  • 官方微信

    官方微信
Copyright  ©  QY  Network  Company  Ltd. All  Rights  Reserved. 2003-2019  群英网络  版权所有   茂名市群英网络有限公司
增值电信经营许可证 : B1.B2-20140078   粤ICP备09006778号
免费拨打  400-678-4567
免费拨打  400-678-4567 免费拨打 400-678-4567 或 0668-2555555
微信公众号
返回顶部
返回顶部 返回顶部