python打开excel表_python读取Excel表格文件的方法

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

2019090214422540.jpg

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

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

直接pip install xlrd安装xlrd库

2019090214422541.jpg

1

2

#引入Excel库的xlrd

import xlrd

#引入Excel库的xlrd

import xlrd

2、获取Excel文件的位置并且读取进来

1

2

3

#导入需要读取Excel表格的路径

data= xlrd.open_workbook(r'C:\Users\NHT\Desktop\Data\\test1.xlsx')

table= data.sheets()[0]

#导入需要读取Excel表格的路径

data = xlrd.open_workbook(r'C:\Users\NHT\Desktop\Data\\test1.xlsx')

table = data.sheets()[0]

3、读取指定的行和列的内容,并将内容存储在列表中(将第三列的时间格式转换)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

#创建一个空列表,存储Excel的数据

tables= []

#将excel表格内容导入到tables列表中

def import_excel(excel):

for rownin 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)

#创建一个空列表,存储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、运行程序

1

2

3

4

5

6

if __name__== '__main__':

#将excel表格的内容导入到列表中

import_excel(table)

#验证Excel文件存储到列表中的数据

for iin tables:

print(i)

if __name__ == '__main__':

#将excel表格的内容导入到列表中

import_excel(table)

#验证Excel文件存储到列表中的数据

for i in tables:

print(i)

5、最终的运行效果如下:

2019090214422542.jpg

6、完整的程序代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

import xlrd

from xlrdimport 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 rownin 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 iin 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读取Excel表格文件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值