在本文中,我们将讨论在python中逐行读取文件的不同方法。
假设我们在与python脚本相同的目录中有一个data.txt文件。让我们看看如何逐行阅读其内容。
小型文件的ag真人游戏的解决方案:使用readlines()获取文件中所有行的列表
第一个基本且效率低下的ag真人游戏的解决方案是使用 readlines() 函数。
如果我们有一个小文件,则可以在文件处理程序上调用readlines(),它将整个文件内容读取到内存中,然后将其拆分为单独的行,并返回文件中所有行的列表。除最后一行外,列表中的所有行将在末尾包含换行符。
例如,
# open file
filehandler = open ("data.txt", "r")
# get list of all lines in file
listoflines = filehandler.readlines()
# close file
filehandler.close()
它将返回文件中的行列表。我们可以遍历该列表,并剥离()新行字符,然后打印该行,即
# iterate over the lines
for line in listoflines:
print(line.strip())
输出:
'''
遇到问题没人解答?小编创建了一个python学习交流qq群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和pdf电子书!
'''
hello
this is a sample
file that contains is
some text
is
like 123
但是,如果文件很大,则会消耗大量内存,因此,如果文件很大,最好避免使用此ag真人游戏的解决方案。
让我们看一些有效的ag真人游戏的解决方案,
使用readline()逐行读取文件
读取大文件时,有效的方法是逐行读取文件,而不是一次性读取所有数据。
让我们将readline()函数与文件处理程序一起使用,即
linestr = filehandler.readline()
readline()返回文件中的下一行,该行的末尾将包含换行符。另外,如果到达文件末尾,则它将返回一个空字符串。
现在让我们看看如何使用readline()逐行读取文件内容
'''
遇到问题没人解答?小编创建了一个python学习交流qq群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和pdf电子书!
'''
# open file
filehandler = open ("data.txt", "r")
while true:
# get next line from file
line = filehandler.readline()
# if line is empty then end of file reached
if not line :
break;
print(line.strip())
# close close
filehandler.close()
输出:
hello
this is a sample
file that contains is
some text
is
like 123
使用上下文管理器逐行读取文件(带块)
当我们打开文件时,我们也需要关闭它。如果我们忘记关闭,那么它将在例如对函数结尾处的文件引用的最后一个引用被破坏时自动关闭。但是,即使文件相关的工作已经完成,如果我们有一个大型功能不会很快结束该怎么办。在这种情况下,我们可以使用上下文管理器自动清除诸如文件关闭之类的内容。
例如,
hello
this is a sample
file that contains is
some text
is
like 123
在这种情况下,当控制脱离块时,文件将自动关闭。即使由于某些异常而出现阻塞。
使用上下文管理器(带块)获取文件中的行列表
让我们遍历文件中的所有行并创建行列表,即
'''
遇到问题没人解答?小编创建了一个python学习交流qq群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和pdf电子书!
'''
# get the all the lines in file in a list
listoflines = list()
with open ("data.txt", "r") as myfile:
for line in myfile:
listoflines.append(line.strip())
listoflines列表的内容为
['hello', 'this is a sample', 'file that contains is', 'some text', 'is', '', 'like 123']
使用上下文管理器和while循环逐行读取文件的内容
让我们使用上下文管理器和while循环遍历文件中的各行,即
# open file
with open("data.txt", "r") as filehandler:
# read next line
line = filehandler.readline()
# check line is not empty
while line:
print(line.strip())
line = filehandler.readline()
列表的内容将是
hello
this is a sample
file that contains is
some text
is
like 123
完整的示例如下,
'''
遇到问题没人解答?小编创建了一个python学习交流qq群:531509025
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和pdf电子书!
'''
def main():
print("****read all lines in file using readlines() *****")
# open file
filehandler = open("data.txt", "r")
# get list of all lines in file
listoflines = filehandler.readlines()
# close file
filehandler.close()
# iterate over the lines
for line in listoflines:
print(line.strip())
print("****read file line by line and then close it manualy *****")
# open file
filehandler = open("data.txt", "r")
while true:
# get next line from file
line = filehandler.readline()
# if line is empty then end of file reached
if not line:
break;
print(line.strip())
# close close
filehandler.close()
print("****read file line by line using with open() *****")
# open file
with open("data.txt", "r") as filehandler:
# read each line in loop
for line in filehandler:
# as each line (except last one) will contain new line character, so strip that
print(line.strip())
print("****read file line by line using with open *****")
# get the all the lines in file in a list
listoflines = list()
with open("data.txt", "r") as myfile:
for line in myfile:
listoflines.append(line.strip())
print(listoflines)
print("****read file line by line using with open() and while loop *****")
# open file
with open("data.txt", "r") as filehandler:
# read next line
line = filehandler.readline()
# check line is not empty
while line:
print(line.strip())
line = filehandler.readline()
if __name__ == '__main__':
main()
输出:
****read all lines in file using readlines() *****
hello
this is a sample
file that contains is
some text
is
like 123
****read file line by line and then close it manualy *****
hello
this is a sample
file that contains is
some text
is
like 123
****read file line by line using with open() *****
hello
this is a sample
file that contains is
some text
is
like 123
****read file line by line using with open *****
['hello', 'this is a sample', 'file that contains is', 'some text', 'is', '', 'like 123']
****read file line by line using with open() and while loop *****
hello
this is a sample
file that contains is
some text
is
like 123