python读取excel单元格内容_python把获得的数据放在一个表格里

python读取excel单元格内容_python把获得的数据放在一个表格里python读取Excel表格文件,例如获取这个文件的数据python读取Excel表格文件,需要如下步骤:1、安装Excel读取数据的库—–xlrd直接pipinstallxlrd安装xlrd库#引入Excel库的xlrdimportxlrd2、获取Excel文件的位置并且读取进来#导入需要读取Excel表格的路径data=xlrd.ope…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

python读取Excel表格文件,例如获取这个文件的数据

python读取excel单元格内容_python把获得的数据放在一个表格里

python读取Excel表格文件,需要如下步骤:

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

直接pip install xlrd安装xlrd库

python读取excel单元格内容_python把获得的数据放在一个表格里

#引入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、最终的运行效果如下:

python读取excel单元格内容_python把获得的数据放在一个表格里

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把获得的数据放在一个表格里

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186548.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号