| by YoungTimes | No comments

Python中逐行读取文件的几种方法

假设在Python脚本的同一个目录下有个文本文件”data.txt”,文件内容为:

She walks in beauty
like the night
Of cloudless climes and starry skies
And all that’s best of dark and bright
Meet in her aspect and her eyes

1.使用readlines()逐行读取文件(针对小文件)

readlines()可以一次性的将文件的所有内容读入内存,然后将文件内容按行分割,返回文件中所有行的list;每行的末尾都会包含一个换行符。

当然这种方式读取小文件是没有问题的,但是对于大文件读取不是一个高效的方案。

# Open file    
fileHandler = open("data.txt", "r")
 
# Get list of all lines in file
listOfLines = fileHandler.readlines()
 
# Close file 
fileHandler.close()

迭代输出每一行的内容,strip()函数去除尾部的换行符。

# Iterate over the lines
for line in listOfLines:
    print(line.strip()) 

程序输出:

She walks in beauty
like the night
Of cloudless climes and starry skies
And all that’s best of dark and bright
Meet in her aspect and her eyes

当要读取的文件很大时,会消耗大量内存,所以在读取大文件时,慎用该方法。

2、使用readline()逐行读取文件

读取大文件高效的方式是逐行读取。readline()返回文件的一行内容,当读取到文件末尾时,返回空字符串。

# 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()   

程序输出内容如下:

She walks in beauty
like the night
Of cloudless climes and starry skies
And all that’s best of dark and bright
Meet in her aspect and her eyes

3、使用上下文管理器(context manager)逐行读取文件

当我们打开文件时,也要记得关闭它。如果忘记关闭,在最后一个对文件句柄的引用(例如在函数末尾)被销毁时,文件将自动关闭。但是设想这样一种场景,在一个大函数中,文件相关的工作已经结束了,但是距离函数末尾还很长,在这种情况下,我们可以使用上下文管理器自动关闭清理文件。

# 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())

程序输出:

She walks in beauty
like the night
Of cloudless climes and starry skies
And all that’s best of dark and bright
Meet in her aspect and her eyes

使用这种方法,当程序退出block之后,文件会自动关闭,即使由于异常退出block,文件也可以自动关闭。

4.使用上下文管理器(Context Manager)和While循环结合实现文件逐行读取

上下文管理器(Context Manager)和While Loop结合读取文件内容。

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()

程序输出如下:

She walks in beauty
like the night
Of cloudless climes and starry skies
And all that’s best of dark and bright
Meet in her aspect and her eyes

发表评论