《Python数据分析技术栈》第01章 05 文件处理(Working with files)
We can use methods or functions in Python to read or write to files. In other words, we can create a file, add content or text to it, and read its contents by using the methods provided by Python.
我们可以使用 Python 中的方法或函数来读取或写入文件。换句话说,我们可以使用 Python 提供的方法创建文件、添加内容或文本并读取其内容。
Here, we discuss how to read and write to comma-separated value (CSV) files. CSV files or comma-separated files are text files that are a text version of an Excel spreadsheet.
在此,我们将讨论如何读写逗号分隔值(CSV)文件。CSV 文件或逗号分隔文件是一种文本文件,是 Excel 电子表格的文本版本。
The functions for all of these operations are defined under the CSV module. This module has to be imported first, using the import csv statement, to use its various methods.
所有这些操作的函数都在 CSV 模块中定义。要使用该模块的各种方法,必须先使用 import csv 语句导入该模块。
Reading from a file from Python involves the following steps:
从 Python 中读取文件涉及以下步骤:
Using the with open statement, we can open an existing CSV file and assign the resulting file object to a variable or file handle (named ‘f’ in the following example). Note that we need to specify the path of the file using either the absolute or relative path. After this, we need to specify the mode for opening the file. For reading, the mode is ‘r’. The file is opened for reading by default if we do not specify a mode.
使用 with open 语句,我们可以打开一个现有的 CSV 文件,并将生成的文件对象分配给一个变量或文件句柄(在下面的示例中命名为 “f”)。请注意,我们需要使用绝对路径或相对路径指定文件路径。然后,我们需要指定打开文件的模式。对于读取,模式为 “r”。如果不指定模式,文件默认为读取模式。
Following this, there is a block of code that starts with storing the contents of the file in a read object, using the csv.reader function where we specify the file handle, f, as an argument.
随后,我们使用 csv.reader 函数,将文件句柄 f 指定为参数,在读取对象中存储文件内容。
However, the contents of this file are not directly accessible through this read object. We create an empty list (named ‘contents’ in the following example), and then we loop through the read object we created in step 2 line by line using a for loop and append it to this list. This list can then be printed to view the lines of the CSV file we created.
但是,通过读取对象并不能直接访问该文件的内容。我们先创建一个空列表(在下面的示例中命名为 “contents”),然后使用 for 循环逐行遍历我们在步骤 2 中创建的读取对象,并将其追加到该列表中。然后就可以打印该列表,查看我们创建的 CSV 文件的各行内容。
# Reading from a file
import csv
with open('animals.csv') as f:
contents = csv.reader(f)
lines_of_file = []
for line in contents:
lines_of_file += line
print(lines_of_file)
Writing to a file involves the following steps.
写入文件包括以下步骤。
Using the open function, open an existing CSV file or if the file does not exist, the open function creates a new file. Pass the name of the file (with the absolute path) in quotes and specify the mode as ‘w’, if you want to overwrite the contents or write into a new file. Use the ‘a’ or ‘append’ mode if you simply want to append some lines to an existing file. Since we do not want to overwrite in this case, we open the file using the append (‘a’) mode. Store it in a variable or file handle and give it a name, let us say ‘f’
使用 open 函数打开现有的 CSV 文件,如果文件不存在,则使用 open 函数创建一个新文件。如果要覆盖文件内容或写入新文件,请用引号传递文件名(带绝对路径),并指定模式为 “w”。如果只想在现有文件中添加一些行,则使用 "a "或 "追加 "模式。在本例中,我们不想覆盖文件,因此使用追加(‘a’)模式打开文件。将其存储在一个变量或文件句柄中,并为其命名,例如 "f
Using the csv.writer() function, create a writer object to add the content since we cannot directly write to the CSV file. Pass the variable (file handle), ‘f’, as an argument to this function.
使用 csv.writer() 函数创建一个 writer 对象来添加内容,因为我们无法直接写入 CSV 文件。将变量(文件句柄)"f "作为参数传递给该函数。
Invoke the writerow method on the writer object created in the previous step. The argument to be passed to this method is the new line to be added (as a list).
在上一步创建的 writer 对象上调用 writerow 方法。传递给该方法的参数是要添加的新行(以列表形式)。
Open the CSV file on your system to see if the changes have been reflected.
在系统上打开 CSV 文件,查看是否已反映更改。
import csv
# Writing to a file
with open(r'animals.csv', "w") as f:
writer_object = csv.writer(f, delimiter=",")
writer_object.writerow(['sheep', 'lamb'])
The modes that can be used with the open function to open a file are:
使用 open 函数打开文件的模式有