python如何读取excel表数据-Python教程

资源魔 47 0

python读取excel表数据的办法:起首装置Excel读取数据的库xlrd;而后猎取Excel文件的地位而且读取出去;接着读取指定的行以及列的内容,并将内容存储正在列表中;最初运转顺序便可。

python读取excel表数据的办法:

一、装置Excel读取数据的库-----xlrd

间接pip install xlrd装置xlrd库

#引入Excel库的xlrd
import xlrd

二、猎取Excel文件的地位而且读取出去

#导入需求读取Excel表格的门路
data = xlrd.open_workbook(r'C:\Users\NHT\Desktop\Data\\test1.xlsx')
table = data.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)
   #将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)

四、运转顺序

if __name__ == '__main__':
  #将excel表格的内容导入到列表中
  import_excel(table)
  #验证Excel文件存储到列表中的数据
  for i in tables:
    print(i)

五、终极的运转成果以下:

六、完好的顺序代码:

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视频教程

以上就是python若何读取excel表数据的具体内容,更多请存眷资源魔其它相干文章!

标签: Python 读取excel python教程 python编程 python使用问题

抱歉,评论功能暂时关闭!