Python 读取excel

121 阅读4分钟

概述

使用pandas和openyxl框架读取sheet工作表数据。

库资源安装

pip install pandas
pip install openyxl

代码示例

import pandas as pd  
from openpyxl import load_workbook  
  
filename = 'C:/Users/DELL/Desktop/温度数据.xlsx'  
workbook = load_workbook(filename)  
sheets = workbook.sheetnames  
for i in range(len(sheets)):  
# 读取Excel文件  
print(sheets[i])  
df = pd.read_excel(filename,sheet_name=sheets[i])  
  
# 获取数据  
print(df)

输出示例

Connected to pydev debugger (build 231.8770.66)
系列一
      温度(单位:℃)                   时间
0          5.5  2023-06-14 14:04:18
1          5.5  2023-06-14 13:59:58
2          5.5  2023-06-14 13:55:38
3          5.5  2023-06-14 13:51:18
4          5.5  2023-06-14 13:46:58
...        ...                  ...
1600       6.8  2023-06-09 15:09:08
1601       6.8  2023-06-09 15:04:51
1602       6.8  2023-06-09 15:00:31
1603       6.8  2023-06-09 14:56:11
1604       6.7  2023-06-09 14:51:51

[1605 rows x 2 columns]
系列二
      温度(单位:℃)                   时间
0          4.7  2023-06-14 14:07:37
1          4.7  2023-06-14 14:03:17
2          4.7  2023-06-14 13:58:57
3          4.7  2023-06-14 13:54:37
4          4.7  2023-06-14 13:50:17
...        ...                  ...
1591       6.9  2023-06-09 15:12:08
1592       6.2  2023-06-09 15:07:48
1593       6.9  2023-06-09 15:01:59
1594       6.7  2023-06-09 14:57:39
1595       7.0  2023-06-09 14:53:19

[1596 rows x 2 columns]
系列三
      温度(单位:℃)                   时间
0          5.6  2023-06-13 17:35:05
1          5.6  2023-06-13 17:30:45
2          5.6  2023-06-13 17:26:25
3          5.6  2023-06-13 17:22:05
4          5.6  2023-06-13 17:17:45
...        ...                  ...
1325       6.9  2023-06-09 15:10:30
1326       6.8  2023-06-09 15:06:04
1327      10.1  2023-06-09 15:01:44
1328      10.4  2023-06-09 14:57:24
1329      10.5  2023-06-09 14:53:04

[1330 rows x 2 columns]
系列四
     温度(单位:℃)                   时间
0         7.1  2023-06-12 07:55:06
1         7.1  2023-06-12 07:50:46
2         7.0  2023-06-12 07:46:26
3         7.0  2023-06-12 07:42:06
4         7.0  2023-06-12 07:37:46
..        ...                  ...
862       7.1  2023-06-09 15:08:22
863      11.2  2023-06-09 15:04:02
864      11.9  2023-06-09 14:59:42
865      12.5  2023-06-09 14:55:22
866      12.2  2023-06-09 14:51:02

[867 rows x 2 columns]
系列五
      温度(单位:℃)                   时间
0          5.3  2023-06-09 15:46:09
1          5.3  2023-06-09 14:33:56
2          5.3  2023-06-09 13:20:25
3          5.4  2023-06-09 12:08:22
4          5.4  2023-06-09 10:56:14
...        ...                  ...
1656       2.3  2023-06-02 14:47:42
1657       2.9  2023-06-02 14:43:22
1658       4.5  2023-06-02 14:39:02
1659       4.6  2023-06-02 14:34:33
1660       4.7  2023-06-02 14:30:13

[1661 rows x 2 columns]
系列六
      温度(单位:℃)                   时间
0          6.1  2023-06-07 10:18:17
1          6.1  2023-06-07 10:13:57
2          6.1  2023-06-07 10:09:37
3          6.1  2023-06-07 10:05:17
4          6.1  2023-06-07 10:00:57
...        ...                  ...
1522       5.1  2023-06-02 14:50:30
1523       5.2  2023-06-02 14:46:03
1524       5.3  2023-06-02 14:41:43
1525       5.3  2023-06-02 14:37:23
1526       5.1  2023-06-02 14:33:03

[1527 rows x 2 columns]
系列七
      温度(单位:℃)                   时间
0          6.6  2023-06-07 07:58:05
1          6.8  2023-06-07 07:53:45
2          6.8  2023-06-07 07:49:25
3          7.3  2023-06-07 07:45:05
4          7.2  2023-06-07 07:40:45
...        ...                  ...
1516       3.0  2023-06-02 14:51:25
1517       4.5  2023-06-02 14:47:05
1518       4.5  2023-06-02 14:42:46
1519       4.6  2023-06-02 14:38:26
1520       4.7  2023-06-02 14:34:06