读写csv文件python_用Python读写CSV文件

读写csv文件python

Let’s face it: you need to get information into and out of your programs through more than just the keyboard and console. Exchanging information through text files is a common way to share info between programs. One of the most popular formats for exchanging data is the CSV format. But how do you use it?

让我们面对现实:您需要通过键盘和控制台以外的方法来获取信息。 通过文本文件交换信息是在程序之间共享信息的常用方法。 交换数据的最流行的格式之一是CSV格式。 但是,您如何使用它呢?

Let’s get one thing clear: you don’t have to (and you won’t) build your own CSV parser from scratch. There are several perfectly acceptable libraries you can use. The Python csv library will work for most cases. If your work requires lots of data or numerical analysis, the pandas library has CSV parsing capabilities as well, which should handle the rest.

让我们澄清一件事:您不必(也不必)从头开始构建自己的CSV解析器。 您可以使用几个完全可以接受的库。 Python csv库适用于大多数情况。 如果您的工作需要大量数据或数值分析,则pandas库也具有CSV解析功能,其余功能应由CSV处理。

In this article, you’ll learn how to read, process, and parse CSV from text files using Python. You’ll see how CSV files work, learn the all-important csv library built into Python, and see how CSV parsing works using the pandas library.

在本文中,您将学习如何使用Python从文本文件中读取,处理和解析CSV。 您将了解CSV文件的工作方式,了解Python内置的最重要的csv库,并了解如何使用pandas库进行CSV解析。

So let’s get started!

因此,让我们开始吧!

什么是CSV文件? (What Is a CSV File?)

A CSV file (Comma Separated Values file) is a type of plain text file that uses specific structuring to arrange tabular data. Because it’s a plain text file, it can contain only actual text data—in other words, printable ASCII or Unicode characters.

CSV文件(逗号分隔值文件)是一种纯文本文件,其使用特定的结构来排列表格数据。 由于它是纯文本文件,因此只能包含实际的文本数据,换句话说,就是可打印的ASCII或Unicode字符。

The structure of a CSV file is given away by its name. Normally, CSV files use a comma to separate each specific data value. Here’s what that structure looks like:

CSV文件的结构由其名称给出。 通常,CSV文件使用逗号分隔每个特定的数据值。 该结构如下所示:

column 1 name,column 2 name, column 3 name
first row data 1,first row data 2,first row data 3
second row data 1,second row data 2,second row data 3
...
column 1 name,column 2 name, column 3 name
first row data 1,first row data 2,first row data 3
second row data 1,second row data 2,second row data 3
...

Notice how each piece of data is separated by a comma. Normally, the first line identifies each piece of data—in other words, the name of a data column. Every subsequent line after that is actual data and is limited only by file size constraints.

请注意每个数据如何用逗号分隔。 通常,第一行标识每条数据,即数据列的名称。 此后的每一行都是实际数据,并且仅受文件大小限制。

In general, the separator character is called a delimiter, and the comma is not the only one used. Other popular delimiters include the tab (t), colon (:) and semi-colon (;) characters. Properly parsing a CSV file requires us to know which delimiter is being used.

通常,分隔符称为定界符,并且逗号不是唯一使用的分隔符。 其他流行定界符包括的标签( t ),结肠癌( : )和分号( ; )字符。 正确解析CSV文件要求我们知道正在使用哪个定界符。

CSV文件来自哪里? (Where Do CSV Files Come From?)

CSV files are normally created by programs that handle large amounts of data. They are a convenient way to export data from spreadsheets and databases as well as import or use it in other programs. For example, you might export the results of a data mining program to a CSV file and then import that into a spreadsheet to analyze the data, generate graphs for a presentation, or prepare a report for publication.

CSV文件通常由处理大量数据的程序创建。 它们是从电子表格和数据库导出数据以及在其他程序中导入或使用数据的便捷方法。 例如,您可以将数据挖掘程序的结果导出到CSV文件,然后将其导入电子表格中以分析数据,为演示文稿生成图形或准备要发布的报告。

CSV files are very easy to work programmatically. Any language that supports text file input and string manipulation (like Python) can work with CSV files directly.

CSV文件很容易以编程方式工作。 任何支持文本文件输入和字符串操作的语言(例如Python)都可以直接使用CSV文件。

使用Python的内置CSV库解析CSV文件 (Parsing CSV Files With Python’s Built-in CSV Library)

The csv library provides functionality to both read from and write to CSV files. Designed to work out of the box with Excel-generated CSV files, it is easily adapted to work with a variety of CSV formats. The csv library contains objects and other code to read, write, and process data from and to CSV files.

csv库提供了读取和写入CSV文件的功能。 专为与Excel生成的CSV文件一起使用而设计,可轻松适应各种CSV格式。 csv库包含用于从CSV文件读取,写入和处理数据的对象和其他代码。

使用csv读取CSV文件 (Reading CSV Files With csv)

Reading from a CSV file is done using the reader object. The CSV file is opened as a text file with Python’s built-in open() function, which returns a file object. This is then passed to the reader, which does the heavy lifting.

使用reader对象可以完成从CSV文件reader 。 CSV文件使用Python的内置open()函数作为文本文件open() ,该函数返回文件对象。 然后将其传递给reader ,由reader完成繁重的工作。

As a reminder, here’s the employee_birthday.txt file:

提醒一下,这是employee_birthday.txt文件:

Here’s code to read it:

这是读取它的代码:

 import import csv
csv
# Use the newLine argument when opening a file.
# Use the newLine argument when opening a file.
with with openopen (( 'employee_birthday.txt''employee_birthday.txt' ) ) as as csvFilecsvFile :
    :
    csv_reader csv_reader = = csvcsv .. readerreader (( csvFilecsvFile , , delimiterdelimiter == ','',' )
    )
    line_count line_count = = 0
    0
    for for row row in in csv_readercsv_reader :
        :
        if if line_count line_count == == 00 :
            :
            printprint (( ff 'Column names are "{", ".join(row)"''Column names are "{", ".join(row)"' )
            )
            line_count line_count += += 1
        1
        elseelse :
            :
            printprint (( ff '' tt {row[0]}{row[0]}  works in the  works in the  {row[1]}{row[1]}  department, and was born in  department, and was born in  {row[2]}{row[2]} .'.' )
            )
            line_count line_count += += 1
    1
    printprint (( ff 'Processed 'Processed  {line_count}{line_count}  lines.' lines.' )
)

This results in the following output:

结果为以下输出:

Each row returned by the reader is a list of String elements containing the data found by removing the delimiters. The first row returned contains the column names, which is handled in a special way.

reader返回的每一行都是String元素列表,其中包含通过删除定界符找到的数据。 返回的第一行包含列名,该列名以特殊方式处理。

使用csv CSV文件读入字典 (Reading CSV Files Into a Dictionary With csv)

Rather than deal with a list of individual String elements, you can read CSV data directly into a dictionary (technically, an Ordered Dictionary) as well.

您可以将CSV数据直接读入字典(从技术上来说,也称为“ 有序字典” ),而不是处理单个String元素的列表。

Again, our input file, employee_birthday.txt is as follows:

同样,我们的输入文件employee_birthday.txt如下:

name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March
name,department,birthday month
John Smith,Accounting,November
Erica Meyers,IT,March

Here’s the code to read it in as a dictionary this time:

这是这次将其作为字典读入的代码:

This results in the same output as before:

结果与之前相同:

 Column names are "name", "department", "birthday month"
Column names are "name", "department", "birthday month"
    John Smith works in the Accounting department, and was born in November.
    John Smith works in the Accounting department, and was born in November.
    Erica Meyers works in the IT department, and was born in March.
    Erica Meyers works in the IT department, and was born in March.
Processed 3 lines.
Processed 3 lines.

Where did the dictionary keys come from? The first line of the CSV file is assumed to contain the keys to use to build the dictionary. If you don’t have these in your CSV file, you should specify your own keys by setting the fieldnames optional parameter to a list containing them.

字典键从何而来? 假定CSV文件的第一行包含用于构建字典的键。 如果CSV文件中没有这些密钥,则应通过将fieldnames可选参数设置为包含它们的列表来指定自己的密钥。

可选的Python CSV reader参数 (Optional Python CSV reader Parameters)

The reader object can handle different styles of CSV files by specifying additional parameters, some of which are shown below:

reader对象可以通过指定其他参数来处理CSV文件的不同样式,其中一些参数如下所示:

  • delimiter specifies the character used to separate each field. The default is the comma (',').

  • quotechar specifies the character used to surround fields that contain the delimiter character. The default is a double quote (' " ').

  • escapechar specifies the character used to escape the delimiter character, in case quotes aren’t used. The default is no escape character.

  • delimiter指定用于分隔每个字段的字符。 默认值为逗号( ',' )。

  • quotechar指定用于包围包含定界符的字段的字符。 默认值为双引号( ' " ' )。

  • escapechar指定用于转义分隔符的字符,以防不使用引号。 默认值为无转义字符。

These parameters deserve some more explanation. Suppose you’re working with the employee_addresses.txt file. Here’s a reminder of how it looks:

这些参数值得更多解释。 假设您正在使用employee_addresses.txt文件。 这是它的外观提醒:

This CSV file contains three fields: name, address, and date joined, which are delimited by commas. The problem is that the data for the address field also contains a comma to signify the zip code.

此CSV文件包含三个字段: nameaddressdate joined ,以逗号分隔。 问题在于address字段的数据还包含一个逗号来表示邮政编码。

There are three different ways to handle this situation:

有三种不同的方法来处理这种情况:

  • Use a different delimiter That way, the comma can safely be used in the data itself. You use the delimiter optional parameter to specify the new delimiter.

  • Wrap the data in quotes The special nature of your chosen delimiter is ignored in quoted strings. Therefore, you can specify the character used for quoting with the quotechar optional parameter. As long as that character also doesn’t appear in the data, you’re fine.

  • Escape the delimiter characters in the data Escape characters work just as they do in format strings, nullifying the interpretation of the character being escaped (in this case, the delimiter). If an escape character is used, it must be specified using the escapechar optional parameter.

  • 使用其他定界符这样,可以在数据本身中安全地使用逗号。 您可以使用delimiter可选参数来指定新的定界符。

  • 将数据括在引号中在引号字符串中将忽略所选定界符的特殊性质。 因此,您可以使用quotechar可选参数指定用于报价的字符。 只要该字符也没有出现在数据中,就可以了。

  • 转义数据中的分隔符转义字符的工作方式与格式字符串中的转义字符相同,从而使对转义字符(在这种情况下为定界符)的解释无效。 如果使用了转义字符,则必须使用escapechar可选参数指定它。

csv编写CSV文件 (Writing CSV Files With csv)

You can also write to a CSV file using a writer object and the .write_row() method:

您还可以使用writer对象和.write_row()方法写入CSV文件:

 import import csv

csv

with with openopen (( 'employee_file.csv''employee_file.csv' , , modemode == 'w''w' ) ) as as employee_fileemployee_file :
    :
    employee_writer employee_writer = = csvcsv .. writerwriter (( employee_fileemployee_file , , delimiterdelimiter == ','',' ,, quotecharquotechar == '"''"' ,, quotingquoting == csvcsv .. QUOTE_MINIMALQUOTE_MINIMAL )

    )

    employee_writeremployee_writer .. writerowwriterow ([([ 'John Smith''John Smith' ,, 'Accounting''Accounting' ,, 'November''November' ])
    ])
    employee_writeremployee_writer .. writerowwriterow ([([ 'Erica Meyers''Erica Meyers' ,, 'IT''IT' ,, 'March''March' ])
])

The quotechar optional parameter tells the writer which character to use to quote fields when writing. Whether quoting is used or not, however, is determined by the quoting optional parameter:

quotechar可选参数告诉writer在编写时使用哪个字符来引用字段。 但是,是否使用引号取决于quoting可选参数:

  • If quoting is set to csv.QUOTE_MINIMAL, then .writerow() will quote fields only if they contain the delimiter or the quotechar. This is the default case.
  • If quoting is set to csv.QUOTE_ALL, then .writerow() will quote all fields.
  • If quoting is set to csv.QUOTE_NONNUMERIC, then .writerow() will quote all fields containing text data and convert all numeric fields to the float data type.
  • If quoting is set to csv.QUOTE_NONE, then .writerow() will escape delimiters instead of quoting them. In this case, you also must provide a value for the escapechar optional parameter.
  • 如果将quoting设置为csv.QUOTE_MINIMAL ,则.writerow()仅在包含delimiterquotechar字段中才引用字段。 这是默认情况。
  • 如果quoting设置为csv.QUOTE_ALL ,则.writerow()将引用所有字段。
  • 如果将quoting设置为csv.QUOTE_NONNUMERIC ,则.writerow()将引用包含文本数据的所有字段,并将所有数字字段转换为float数据类型。
  • 如果将quoting设置为csv.QUOTE_NONE ,则.writerow()将转义分隔符而不是使用引号。 在这种情况下,还必须为escapechar可选参数提供一个值。

Reading the file back in plain text shows that the file is created as follows:

以纯文本方式读回该文件表明该文件的创建过程如下:

使用csv从字典写入CSV文件 (Writing CSV File From a Dictionary With csv)

Since you can read our data into a dictionary, it’s only fair that you should be able to write it out from a dictionary as well:

由于您可以将我们的数据读入字典中,因此也应该能够从字典中将其写出来是很公平的:

 import import csv

csv

with with openopen (( 'employee_file2.csv''employee_file2.csv' , , modemode == 'w''w' ) ) as as csvfilecsvfile :
    :
    fieldnames fieldnames = = [[ 'emp_name''emp_name' , , 'dept''dept' , , 'birth_month''birth_month' ]
    ]
    writer writer = = csvcsv .. DictWriterDictWriter (( csvfilecsvfile , , fieldnamesfieldnames == fieldnamesfieldnames )

    )

    writerwriter .. writeheaderwriteheader ()
    ()
    writerwriter .. writerowwriterow ({({ 'emp_name''emp_name' : : 'John Smith''John Smith' , , 'dept''dept' : : 'Accounting''Accounting' , , 'birth_month''birth_month' :: 'November''November' })
    })
    writerwriter .. writerowwriterow ({({ 'emp_name''emp_name' : : 'Erica Meyers''Erica Meyers' , , 'dept''dept' : : 'IT''IT' , , 'birth_month''birth_month' :: 'March''March' })
})

Unlike DictReader, the fieldnames parameter is required when writing a dictionary. This makes sense, when you think about it: without a list of fieldnames, the DictWriter can’t know which keys to use to retrieve values from your dictionaries. It also uses the keys in fieldnames to write out the first row as column names.

DictReader不同,编写字典时需要fieldnames参数。 当您考虑时,这是有道理的:如果没有fieldnames列表,则DictWriter无法知道用于从字典中检索值的键。 它还使用fieldnames的键将第一行写为列名。

The code above generates the following output file:

上面的代码生成以下输出文件:

pandas库解析CSV文件 (Parsing CSV Files With the pandas Library)

Of course, the Python CSV library isn’t the only game in town. Reading CSV files is possible in pandas as well. It is highly recommended if you have a lot of data to analyze.

当然,Python CSV库并不是该镇唯一的游戏。 pandas也可以读取CSV文件。 如果您要分析大量数据,强烈建议您使用。

pandas is an open-source Python library that provides high performance data analysis tools and easy to use data structures. pandas is available for all Python installations, but it is a key part of the Anaconda distribution and works extremely well in Jupyter notebooks to share data, code, analysis results, visualizations, and narrative text.

pandas是一个开源Python库,它提供高性能的数据分析工具和易于使用的数据结构。 pandas适用于所有Python安装,但它是Anaconda发行版的关键部分,在Jupyter笔记本中非常有用,可以共享数据,代码,分析结果,可视化效果和叙述文本。

Installing pandas and its dependencies in Anaconda is easily done:

Anaconda安装pandas及其依赖项很容易:

 $ conda install pandas
$ conda install pandas

As is using pip/pipenv for other Python installations:

与在其他Python安装中使用pip / pipenv

We won’t delve into the specifics of how pandas works or how to use it. For an in-depth treatment on using pandas to read and analyze large data sets, check out Shantnu Tiwari’s superb article on working with large Excel files in pandas.

我们不会深入研究pandas如何工作或如何使用它。 有关使用pandas读取和分析大数据集的深入处理,请查看Shantnu Tiwari撰写的关于在熊猫中使用大型Excel文件 的精彩文章。

使用pandas读取CSV文件 (Reading CSV Files With pandas)

To show some of the power of pandas CSV capabilities, I’ve created a slightly more complicated file to read, called hrdata.csv. It contains data on company employees:

为了展示pandas CSV功能的某些功能,我创建了一个稍微复杂的文件hrdata.csv来读取。 它包含有关公司员工的数据:

Name,Hire Date,Salary,Sick Days remaining
Graham Chapman,03/15/14,50000.00,10
John Cleese,06/01/15,65000.00,8
Eric Idle,05/12/14,45000.00,10
Terry Jones,11/01/13,70000.00,3
Terry Gilliam,08/12/14,48000.00,7
Michael Palin,05/23/13,66000.00,8
Name,Hire Date,Salary,Sick Days remaining
Graham Chapman,03/15/14,50000.00,10
John Cleese,06/01/15,65000.00,8
Eric Idle,05/12/14,45000.00,10
Terry Jones,11/01/13,70000.00,3
Terry Gilliam,08/12/14,48000.00,7
Michael Palin,05/23/13,66000.00,8

Reading the CSV into a pandas DataFrame is quick and straightforward:

将CSV读取到pandas DataFrame是快速而直接的:

That’s it: three lines of code, and only one of them is doing the actual work. pandas.read_csv() opens, analyzes, and reads the CSV file provided, and stores the data in a DataFrame. Printing the DataFrame results in the following output:

就是这样:三行代码,只有其中一行正在执行实际工作。 pandas.read_csv()打开,分析和读取提供的CSV文件,并将数据存储在DataFrame中 。 打印DataFrame得到以下输出:

              Name Hire Date   Salary  Sick Days remaining
             Name Hire Date   Salary  Sick Days remaining
0  Graham Chapman  03/15/14  50000.0                   10
0  Graham Chapman  03/15/14  50000.0                   10
1     John Cleese  06/01/15  65000.0                    8
1     John Cleese  06/01/15  65000.0                    8
2       Eric Idle  05/12/14  45000.0                   10
2       Eric Idle  05/12/14  45000.0                   10
3     Terry Jones  11/01/13  70000.0                    3
3     Terry Jones  11/01/13  70000.0                    3
4   Terry Gilliam  08/12/14  48000.0                    7
4   Terry Gilliam  08/12/14  48000.0                    7
5   Michael Palin  05/23/13  66000.0                    8
5   Michael Palin  05/23/13  66000.0                    8

Here are a few points worth noting:

这里有几点值得注意:

  • First, pandas recognized that the first line of the CSV contained column names, and used them automatically. I call this Goodness.
  • However, pandas is also using zero-based integer indices in the DataFrame. That’s because we didn’t tell it what our index should be.
  • Further, if you look at the data types of our columns , you’ll see pandas has properly converted the Salary and Sick Days remaining columns to numbers, but the Hire Date column is still a String. This is easily confirmed in interactive mode:

  • 首先, pandas认识到CSV的第一行包含列名,并自动使用它们。 我称之为善良。
  • 但是, pandasDataFrame也使用了从零开始的整数索引。 那是因为我们没有告诉它我们的索引应该是什么。
  • 此外,如果您查看我们列的数据类型,您会发现pandas已将SalarySick Days remaining列正确转换为数字,但Hire Date列仍然是String 。 在交互模式下很容易确认:

     >>>  print ( type ( df [ 'Hire Date' ][ 0 ]))
    
    

Let’s tackle these issues one at a time. To use a different column as the DataFrame index, add the index_col optional parameter:

让我们一次解决这些问题。 要将其他列用作DataFrame索引,请添加index_col可选参数:

Now the Name field is our DataFrame index:

现在,“ Name字段是我们的DataFrame索引:

                Hire Date   Salary  Sick Days remaining
               Hire Date   Salary  Sick Days remaining
Name                                                  
Name                                                  
Graham Chapman  03/15/14  50000.0                   10
Graham Chapman  03/15/14  50000.0                   10
John Cleese     06/01/15  65000.0                    8
John Cleese     06/01/15  65000.0                    8
Eric Idle       05/12/14  45000.0                   10
Eric Idle       05/12/14  45000.0                   10
Terry Jones     11/01/13  70000.0                    3
Terry Jones     11/01/13  70000.0                    3
Terry Gilliam   08/12/14  48000.0                    7
Terry Gilliam   08/12/14  48000.0                    7
Michael Palin   05/23/13  66000.0                    8
Michael Palin   05/23/13  66000.0                    8

Next, let’s fix the data type of the Hire Date field. You can force pandas to read data as a date with the parse_dates optional parameter, which is defined as a list of column names to treat as dates:

接下来,让我们修复“ Hire Date字段的数据类型。 您可以使用parse_dates可选参数(定义为将其视为日期的列名列表)来强制pandas将日期读取为数据:

Notice the difference in the output:

注意输出的差异:

                 Hire Date   Salary  Sick Days remaining
                Hire Date   Salary  Sick Days remaining
Name                                                   
Name                                                   
Graham Chapman 2014-03-15  50000.0                   10
Graham Chapman 2014-03-15  50000.0                   10
John Cleese    2015-06-01  65000.0                    8
John Cleese    2015-06-01  65000.0                    8
Eric Idle      2014-05-12  45000.0                   10
Eric Idle      2014-05-12  45000.0                   10
Terry Jones    2013-11-01  70000.0                    3
Terry Jones    2013-11-01  70000.0                    3
Terry Gilliam  2014-08-12  48000.0                    7
Terry Gilliam  2014-08-12  48000.0                    7
Michael Palin  2013-05-23  66000.0                    8
Michael Palin  2013-05-23  66000.0                    8

The date is now formatted properly, which is easily confirmed in interactive mode:

日期现在已正确格式化,可以在交互模式下轻松确认:

If your CSV files doesn’t have column names in the first line, you can use the names optional parameter to provide a list of column names. You can also use this if you want to override the column names provided in the first line. In this case, you must also tell pandas.read_csv() to ignore existing column names using the header=0 optional parameter:

如果您的CSV文件的第一行中没有列名,则可以使用names可选参数来提供列名列表。 如果要覆盖第一行中提供的列名,也可以使用此名称。 在这种情况下,还必须使用header=0可选参数告诉pandas.read_csv()忽略现有的列名:

 import import pandas
pandas
df df = = pandaspandas .. read_csvread_csv (( 'hrdata.csv''hrdata.csv' , 
            , 
            index_colindex_col == 'Employee''Employee' , 
            , 
            parse_datesparse_dates == [[ 'Hired''Hired' ], 
            ], 
            headerheader == 00 , 
            , 
            namesnames == [[ 'Employee''Employee' , , 'Hired''Hired' ,, 'Salary''Salary' ,, 'Sick Days''Sick Days' ])
])
printprint (( dfdf )
)

Notice that, since the column names changed, the columns specified in the index_col and parse_dates optional parameters must also be changed. This now results in the following output:

请注意,由于列名已更改,因此还必须更改index_colparse_dates可选参数中指定的列。 现在这将导致以下输出:

pandas写CSV文件 (Writing CSV Files With pandas)

Of course, if you can’t get your data out of pandas again, it doesn’t do you much good. Writing a DataFrame to a CSV file is just as easy as reading one in. Let’s write the data with the new column names to a new CSV file:

当然,如果您无法再次从pandas获取数据,那对您没有多大帮助。 将DataFrame写入CSV文件就像读入一样容易。让我们将具有新列名的数据写入新的CSV文件:

 import import pandas
pandas
df df = = pandaspandas .. read_csvread_csv (( 'hrdata.csv''hrdata.csv' , 
            , 
            index_colindex_col == 'Employee''Employee' , 
            , 
            parse_datesparse_dates == [[ 'Hired''Hired' ],
            ],
            headerheader == 00 , 
            , 
            namesnames == [[ 'Employee''Employee' , , 'Hired''Hired' ,, 'Salary''Salary' ,, 'Sick Days''Sick Days' ])
])
dfdf .. to_csvto_csv (( 'hrdata_modified.csv''hrdata_modified.csv' )
)

The only difference between this code and the reading code above is that the print(df) call was replaced with df.to_csv(), providing the file name. The new CSV file looks like this:

此代码与上面的阅读代码之间的唯一区别是,使用提供文件名的df.to_csv()代替了print(df)调用。 新的CSV文件如下所示:

结论 (Conclusion)

If you understand the basics of reading CSV files, then you won’t ever be caught flat footed when you need to deal with importing data. Most CSV reading, processing, and writing tasks can be easily handled by the basic csv Python library. If you have a lot of data to read and process, the pandas library provides quick and easy CSV handling capabilities as well.

如果您了解读取CSV文件的基础知识,那么当您需要处理导入数据时,就永远不会措手不及。 基本的csv Python库可以轻松处理大多数CSV读取,处理和写入任务。 如果您有大量数据要读取和处理, pandas库也提供了快速简便的CSV处理功能。

Are there other ways to parse text files? Of course! Libraries like ANTLR, PLY, and PlyPlus can all handle heavy-duty parsing, and if simple String manipulation won’t work, there are always regular expressions.

还有其他解析文本文件的方法吗? 当然! 像ANTLR , PLY和PlyPlus这样的库都可以处理繁重的解析,并且如果简单的String操作不起作用,则总会有正则表达式。

But those are topics for other articles…

但是这些是其他文章的主题...

翻译自: https://www.pybloggers.com/2018/07/reading-and-writing-csv-files-in-python/

读写csv文件python

你可能感兴趣的:(字符串,python,java,大数据,数据分析)