如何在 Python 中将 Excel 转换为 CSV?

2025年1月5日 | 阅读 7 分钟

Excel 文件是由 Excel 或其他电子表格程序创建的电子表格。文件类型是 Excel 二进制文件格式。Excel 文件的格式是 xlsx,存储在 Excel 文件中的数据是二进制流,复合文件。有关 Excel 工作簿的数据和结构的信息包含在流和子流中。xlsx 格式在 Excel 2007 版本之后成为默认格式,因为它是一种更开放、更结构化的格式。许多类型的数据可以导出为包含 PDF、TXT、超文本标记语言、XPS 和 XLSX 的格式。Excel 在市场上有很多竞争对手,例如 Google Sheets、Numbers 和 Apache Open Office Cal。Excel 于 1985 年由 Microsoft 发布,名称为 Microsoft Multiplan。

另一方面,CSV 文件(也称为逗号分隔值)将数据存储在逗号分隔的格式中,并使用换行符分隔记录。CSV 文件通常用于电子表格和数据库。CSV 文件可用于在程序之间移动数据,而这些程序通常无法共享数据。CSV 文件提供了一种在无法共享的程序之间共享结构化信息(如电子表格内容)的方法。

让我们看看如何在 Python 中打开 CSV 文件和 Excel 文件。

在 Python 中打开 Excel 文件

代码

输出

        Date        Day                                               Topic    Words
0  2024-04-01     Monday                          Algorithm to Solve sudoku    734.0
1  2024-04-02    Tuesday                          Algorithm to Solve sudoku    790.0
2  2024-04-03  Wednesday                          Algorithm to Solve Sudoku 723.0
3  2024-04-04   Thursday                          Algorithm to Solve sudoku    941.0
4  2024-04-05     Friday                                                NaN      NaN
5  2024-04-06   Saturday           Split Pandas DataFrame by Rows in Python    720.0
6  2024-04-07     Sunday                                                NaN      NaN
7  2024-04-08     Monday           Split Pandas DataFrame by Rows in Python    510.0
8  2024-04-09    Tuesday                                                NaN      NaN
9  2024-04-10  Wednesday     Python requests - SSL Certificate Verification    772.0
10 2024-04-11   Thursday     Python requests - SSL Certificate Verification    701.0
11 2024-04-12     Friday     Python requests - SSL Certificate Verification    518.0
12 2024-04-13   Saturday                                                NaN      NaN
13 2024-04-14     Sunday                                                NaN      NaN
14 2024-04-15     Monday  How to call a Variable from Another Function i...    713.0
15 2024-04-16    Tuesday  How to call a Variable from Another Function i...    700.0
16 2024-04-17  Wednesday   Convert a datetime to a UTC timestamp in Pyth...    728.0
17 2024-04-18   Thursday  How to access the serial (RS232) port in Pytho...    702.0
18 2024-04-19     Friday  How to create a .pyc file in Python?(264) + Py...    547.0
19 2024-04-20   Saturday  How to convert a string to a Python class object?    768.0
20 2024-04-21     Sunday                                                NaN      NaN
21 2024-04-22     Monday  How do you convert a string to a Python class object?    538.0
22 2024-04-23    Tuesday                                                NaN      NaN
23 2024-04-24  Wednesday                                                NaN      NaN
24 2024-04-25   Thursday                                                NaN      NaN
25 2024-04-26     Friday                                                NaN      NaN
26 2024-04-27   Saturday                                                NaN      NaN
27 2024-04-28     Sunday                                                NaN      NaN
28 2024-04-29     Monday                                                NaN      NaN
29 2024-04-30    Tuesday                                                NaN      NaN
30        NaT        NaN                                                NaN      NaN
31        NaT        NaN                                                NaN      NaN
32        NaT        NaN                                  Total Word counts  11105.0
33        NaT        NaN                                       Total files       8.0

说明

在上面的代码中,使用 pandas 库打开 Excel 文件,使用 read_excel 方法,并将 Excel 文件传递给函数。然后打印 Excel 文件中的数据。

在 Python 中打开 CSV 文件

以下代码演示了如何在 Python 中打开 CSV 文件。

代码

输出

   Category                                            Message
0       ham  Go until Jurong point, crazy. Available only ...
1       ham                      Ok lar... Joking with u oni...
2      spam  Free entry in 2 a wkly comp to win FA Cup fina...
3       ham  U dun say so early hor... U can already then say...
4       ham  Nah I don't think he goes to usf, he lives aro...
5      spam  FreeMsg Hey there darling it's been 3 week's n...
6       ham  Even my brother is not like to speak with me. ...
7       ham  As per your request 'Melle Melle (Oru Minnamin...
8      spam  WINNER!! As a valued network customer, you have...
9      spam. Had your mobile 11 months or more? U R entitled...
10      ham, I'm gonna be home soon, and i don't want to talk...
11     spam  SIX chances to win CASH! From 100 to 20,000 po...
12     spam  URGENT! You have won a 1 week FREE membership ...
13      ham  I've been searching for the right words to tha...
14      ham                I HAVE A DATE ON SUNDAY WITH WILL!!
15     spam  XXXMobileMovieClub: To use your credit, click ...
16      ham                         Oh k...I'm watching here:)
17      ham  Eh u remember how 2 spell his name... Yes, i do...
18      ham  Fine if thats the way u feel. Thats the way ...
19     spam  England v Macedonia - dont miss the goals/team...

在 Python 中将 Excel 转换为 CSV

考虑一个包含一些随机数据的 Excel 文件。任务是将 Excel 文件转换为 CSV 文件。

文件:Daily and Weekly Report April.xlsx

有许多方法可以将 Excel 文件转换为 CSV 文件。

1. 使用 Pandas 模块将 Excel 转换为 CSV

让我们看看如何使用 pandas 模块将上面的 Excel 文件转换为 CSV。

代码

输出

         Date        Day                                               Topic  Words
0   2024-04-01     Monday                          Algorithm to Solve sudoku  734.0
1   2024-04-02    Tuesday                          Algorithm to Solve sudoku  790.0
2   2024-04-03  Wednesday                          Algorithm to Solve sudoku  723.0
3   2024-04-04   Thursday                          Algorithm to Solve sudoku  941.0
4   2024-04-05     Friday                                                NaN    NaN
5   2024-04-06   Saturday           Split Pandas DataFrame by Rows in Python  720.0
6   2024-04-07     Sunday                                                NaN    NaN
7   2024-04-08     Monday           Split Pandas DataFrame by Rows in Python  510.0
8   2024-04-09    Tuesday                                                NaN    NaN
9   2024-04-10  Wednesday     Python requests - SSL Certificate Verification  772.0
10  2024-04-11   Thursday     Python requests - SSL Certificate Verification  701.0
11  2024-04-12     Friday     Python requests - SSL Certificate Verification  518.0
12  2024-04-13   Saturday                                                NaN    NaN
13  2024-04-14     Sunday                                                NaN    NaN
14  2024-04-15     Monday  How to call a Variable from Another Function i...  713.0

说明

在上面的代码中,导入了 pandas 模块,并将 Excel 文件作为输入,然后使用 read_excel 函数读取 Excel 文件。通过 passing Excel 文件名,Excel 文件被转换为 CSV 文件,方法是使用 object.to_csv() 函数,在函数中,index 选项设置为 none,header 选项也设置为 none。然后读取转换后的 CSV 文件,并打印 dataframe 对象。

2. 使用 openpyxl 和 CSV 模块将 Excel 文件转换为 CSV

代码

CSV 文件数据

说明

在上面的代码中,导入了 openpyxl 和 csv 模块,并在 load_workbook 函数中输入 Excel 文件数据。获取活动工作表,并使用 writer 函数将 Excel 文件的数据转换为 CSV 文件,分隔符为逗号“,”,并将 Excel 文件的每一行保存到 CSV 文件中。

结论

Excel 文件和 CSV 文件都用于保存或存储数据,但在 Excel 文件中,数据可以存储在工作表中,而在 CSV 文件(也称为逗号分隔值)中,数据以逗号分隔,可用于分析数据。有许多方法可以将 Excel 数据转换为 CSV 文件。使用 pandas 模块和 openpyxl 模块,可以将 Excel 文件转换为 CSV 文件。openpyxl 模块可以正常工作,并设置为使用活动工作表。因此,这就是 Excel 文件可以转换为 CSV 文件的方式。