python如何读取excel表数据


本文摘自php中文网,作者coldplay.xixi,侵删。

python读取excel表数据的方法:首先安装Excel读取数据的库xlrd;然后获取Excel文件的位置并且读取进来;接着读取指定的行和列的内容,并将内容存储在列表中;最后运行程序即可。

python读取excel表数据的方法:

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

直接pip install xlrd安装xlrd库

1

2

#引入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]

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 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 i in tables:

    print(i)

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

阅读剩余部分

相关阅读 >>

Python 绘图库 matplotlib 入门教程_Python

怎么获得一个字符串的子串

Python中的array数组模块相关使用

怎么用Python

Python中reverse()的用法是什么?

Python中lambda函数的简单分析(附示例)

Python如何计时

Python使用迭代器捕获generator返回值的方法

Python并发之poolexecutor的介绍(附示例)

安装Python后如何打开

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...