python 读入文件列表

In this tutorial we are going to see how we can read a file and store the content of the file into a python list. While working with python many a times data is stored into text files or csv files and to use that data into our code it must be brought to the python code.

在本教程中,我们将了解如何读取文件并将文件内容存储到python列表中。 在使用python的过程中,数据经常存储在文本文件或csv文件中,并且要将这些数据用于我们的代码中,必须将其带到python代码中。

In this tutorial we will see different methods on how it can be done efficiently and with as little code as possible.

在本教程中,我们将看到有关如何以尽可能少的代码有效完成它的不同方法。

Python将文件读入列表 (Python Read File Into List)

与关键字一起使用 (Using with Keyword)

We can use the with keyword provided by python for our job. First we need to open the file with the open() method which will take the filepath as argument and return a file descriptor to the file. We can then loop over all the lines in the file and append them one by one to our list. The code will look as below:

我们可以使用python提供的with关键字来完成工作。 首先,我们需要使用open()方法打开文件,该方法将文件路径作为参数,并向文件返回文件描述符。 然后,我们可以遍历文件中的所有行,并将它们一行一行地追加到列表中。 该代码将如下所示:

with open("./readfile.txt") as file :
 
	for line in file :
	line.strip()
    	lines.append(line)
 
print(lines)

Note: Watch your backslashes in windows path names, as those are also escape chars in strings. You can use forward slashes or double backslashes instead.

注意 :在Windows路径名中 注意 反斜杠,因为它们也是字符串中的转义字符。 您可以改用正斜杠或双反斜杠。

The strip method is only used to remove any whitespace characters like \n at the end of the lines.

strip方法仅用于删除行尾的所有空白字符,例如\ n。

There is a small problem with the above code, once opened we cannot close the file again so it is advised to first open file using a file descriptor which can be then used to close the same.

上面的代码有一个小问题,一旦打开我们就无法再次关闭文件,因此建议您首先使用文件描述符打开文件,然后再使用该文件描述符将其关闭。

lines = []
file = open("./readfile.txt")
for line in file :
	lines.append(line)
 
file.close()
print(lines)

The code can further shortened as:

该代码可以进一步缩短为:

file = open("./readfile.txt")
lines = [line for line in file]
file.close()
print(lines)

读取文件的传统方法 (Traditional Method of Reading File)

We can use the traditional method where we will first read the file and separate the file when we encounter some special character. If we want to split the file line by line the special character will be \n. If we want to split the file word by word then we can use space as a special character. The code will look like : 

我们可以使用传统方法,在遇到某些特殊字符时,我们将首先读取文件并分离文件。 如果我们要逐行分割文件,则特殊字符为\ n。 如果我们想逐个文件分割文件,则可以使用 空格 作为特殊字符。 该代码将如下所示:

file = open("./readfile.txt")
lines = file.read().split('\n')
file.close()
print(lines)

使用readlines()方法 (Using readlines() Method)

The readlines() method can also be used directly to read lines and store them into a list in python. The code for the same will look as below: 

readlines()方法也可以直接用于读取行并将其存储在python中的列表中。 相同的代码如下所示:

file = open("./readfile.txt")
lines = file.readlines()
file.close()
print(lines)

readlines() will not remove the \n at the end of the file so if we want the \n to be removed we again need to use the strip method to get the work done.

readlines()不会在文件末尾删除\ n,因此,如果我们希望删除\ n,我们再次需要使用strip方法来完成工作。

使用splitlines()方法 (Using splitlines() Method)

The splitlines method will strip all the whitespace characters at the end of the lines and return a list containing the lines of the file.

splitlines方法将在行尾去除所有空白字符,并返回包含文件行的列表。

file = open("./readfile.txt")
lines = file.read().splitlines()
file.close()
print(lines)

使用list()方法: (Using list() Method:)

We can use the list method provided by python to convert a file into list. list method will not remove the \n at the end of the file.

我们可以使用python提供的list方法将文件转换为list。 list方法不会删除文件末尾的\ n。

file = open("./readfile.txt")
lines = list(file)
file.close()
print(lines)

使用tuple()方法 (Using tuple() Method)

tuple can take an iterator and instantiate a tuple instance for you from the iterator that you give it. lines is a tuple created from the lines of the file. This will yield an array of lines from the file.

元组可以使用一个迭代器,并从提供它的迭代器为您实例化一个元组实例。 lines是从文件的各行创建的元组。 这将产生 从所述文件中的行的 阵列

file = open("./readfile.txt")
lines = tuple(file)
file.close()
print(lines)

The use of method depends on the application on which python is used. splitlines() method is most commonly used for splitting a file into lines. The split() method is considered more generic as it allows split lines using the character of our choice and not just when a new line appears.

方法的使用取决于使用python的应用程序。 splitlines()方法最常用于将文件分割成几行。 split()方法被认为是更通用的方法,因为它允许使用我们选择的字符来分割行,而不仅仅是出现新行时。

翻译自: https://www.thecrazyprogrammer/2020/02/python-read-file-into-list.html

python 读入文件列表

更多推荐

python 读入文件列表_Python将文件读入列表