python按行读取文件
转载:https://blog.csdn.net/qdPython/article/details/106160272
在本文中,我们将讨论在Python中逐行读取文件的不同方法。
假设我们在与python脚本相同的目录中有一个data.txt文件。让我们看看如何逐行阅读其内容。
小型文件的解决方案:使用readlines()获取文件中所有行的列表
第一个基本且效率低下的解决方案是使用 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
但是,如果文件很大,则会消耗大量内存,因此,如果文件很大,最好避免使用此解决方案。
让我们看一些有效的解决方案,
使用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
参考:
https://www.cnblogs.com/Kingfan1993/p/9570079.html
https://www.jianshu.com/p/3d3e17fa5a14
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· DeepSeek “源神”启动!「GitHub 热点速览」
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器