如何在Python中读取文件的每一行,并将每一行存储为列表中的元素?
我想逐行读取文件,并将每一行附加到列表的末尾。
如何在Python中读取文件的每一行,并将每一行存储为列表中的元素?
我想逐行读取文件,并将每一行附加到列表的末尾。
当前回答
命令行版本
#!/bin/python3
import os
import sys
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
filename = dname + sys.argv[1]
arr = open(filename).read().split("\n")
print(arr)
运行方式:
python3 somefile.py input_file_name.txt
其他回答
f = open("your_file.txt",'r')
out = f.readlines() # will append in the list out
现在,变量out是您想要的列表(数组)。您可以选择:
for line in out:
print (line)
Or:
for line in f:
print (line)
你会得到同样的结果。
将文件行读取到列表中的干净和Python方式
首先也是最重要的一点,你应该专注于以一种高效的方式打开文件并阅读其内容。下面是一个我个人不喜欢的方式的例子:
infile = open('my_file.txt', 'r') # Open the file for reading.
data = infile.read() # Read the contents of the file.
infile.close() # Close the file since we're done using it.
相反,我更喜欢以下打开文件的方法,既可以读也可以写非常干净,不需要关闭文件的额外步骤一旦您使用完它。在下面的语句中,我们将打开文件用于读取,并将其分配给变量infile一旦代码在此语句已完成运行,文件将自动关闭。
# Open the file for reading.
with open('my_file.txt', 'r') as infile:
data = infile.read() # Read the contents of the file into memory.
现在我们需要专注于将这些数据引入Python列表,因为它们是可迭代的、高效的和灵活的。在您的案例中,期望的目标是将文本文件的每一行放入一个单独的元素中。为此,我们将使用splitlines()方法,如下所示:
# Return a list of the lines, breaking at line boundaries.
my_list = data.splitlines()
最终产品:
# Open the file for reading.
with open('my_file.txt', 'r') as infile:
data = infile.read() # Read the contents of the file into memory.
# Return a list of the lines, breaking at line boundaries.
my_list = data.splitlines()
测试我们的代码:
文本文件的内容:
A fost odatã ca-n povesti,
A fost ca niciodatã,
Din rude mãri împãrãtesti,
O prea frumoasã fatã.
打印测试报表:
print my_list # Print the list.
# Print each line in the list.
for line in my_list:
print line
# Print the fourth element in this list.
print my_list[3]
输出(因unicode字符而不同):
['A fost odat\xc3\xa3 ca-n povesti,', 'A fost ca niciodat\xc3\xa3,',
'Din rude m\xc3\xa3ri \xc3\xaemp\xc3\xa3r\xc3\xa3testi,', 'O prea
frumoas\xc3\xa3 fat\xc3\xa3.']
A fost odatã ca-n povesti, A fost ca niciodatã, Din rude mãri
împãrãtesti, O prea frumoasã fatã.
O prea frumoasã fatã.
这段代码将把整个文件读入内存,并删除每行末尾的所有空白字符(换行符和空格):
with open(filename) as file:
lines = [line.rstrip() for line in file]
如果您正在处理一个大文件,那么您应该逐行读取并处理它:
with open(filename) as file:
for line in file:
print(line.rstrip())
在Python 3.8及以上版本中,可以使用while循环和walrus运算符,如下所示:
with open(filename) as file:
while (line := file.readline().rstrip()):
print(line)
根据您计划对文件执行的操作以及文件的编码方式,您可能还需要手动设置访问模式和字符编码:
with open(filename, 'r', encoding='UTF-8') as file:
while (line := file.readline().rstrip()):
print(line)
这比必要的更明确,但可以做到你想要的。
with open("file.txt") as file_in:
lines = []
for line in file_in:
lines.append(line)
下面是我用来简化文件I/O的Python(3)助手库类:
import os
# handle files using a callback method, prevents repetition
def _FileIO__file_handler(file_path, mode, callback = lambda f: None):
f = open(file_path, mode)
try:
return callback(f)
except Exception as e:
raise IOError("Failed to %s file" % ["write to", "read from"][mode.lower() in "r rb r+".split(" ")])
finally:
f.close()
class FileIO:
# return the contents of a file
def read(file_path, mode = "r"):
return __file_handler(file_path, mode, lambda rf: rf.read())
# get the lines of a file
def lines(file_path, mode = "r", filter_fn = lambda line: len(line) > 0):
return [line for line in FileIO.read(file_path, mode).strip().split("\n") if filter_fn(line)]
# create or update a file (NOTE: can also be used to replace a file's original content)
def write(file_path, new_content, mode = "w"):
return __file_handler(file_path, mode, lambda wf: wf.write(new_content))
# delete a file (if it exists)
def delete(file_path):
return os.remove() if os.path.isfile(file_path) else None
然后使用FileIO.lines函数,如下所示:
file_ext_lines = FileIO.lines("./path/to/file.ext"):
for i, line in enumerate(file_ext_lines):
print("Line {}: {}".format(i + 1, line))
请记住,mode(默认为“r”)和filter_fn(默认为检查空行)参数是可选的。
您甚至可以删除read、write和delete方法,只保留FileIO.line,甚至将其转换为一个单独的方法read_lines。