我什么时候应该使用 file.read() 或 file.readlines()? [英] When should I ever use file.read() or file.readlines()?

查看:22
本文介绍了我什么时候应该使用 file.read() 或 file.readlines()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,如果我对打开的文件进行迭代,在不读取"它的情况下对其进行迭代要快得多.

I noticed that if I iterate over a file that I opened, it is much faster to iterate over it without "read"-ing it.

l = open('file','r')
for line in l:
    pass (or code)

l = open('file','r')
for line in l.read() / l.readlines():
    pass (or code)

第二个循环将花费大约 1.5 倍的时间(我在完全相同的文件上使用了 timeit,结果是 0.442 与 0.660),并且会给出相同的结果.

The 2nd loop will take around 1.5x as much time (I used timeit over the exact same file, and the results were 0.442 vs. 0.660), and would give the same result.

那么 - 我什么时候应该使用 .read() 或 .readlines()?

So - when should I ever use the .read() or .readlines()?

因为我总是需要遍历我正在阅读的文件,并且在艰难地学习了 .read() 在大数据上的缓慢程度之后 - 我似乎无法想象再次使用它.

Since I always need to iterate over the file I'm reading, and after learning the hard way how painfully slow the .read() can be on large data - I can't seem to imagine ever using it again.

推荐答案

对您的问题的简短回答是,这三种读取文件位的方法中的每一种都有不同的用例.如上所述,f.read() 将文件作为单独的字符串读取,因此允许相对简单的文件范围操作,例如文件范围的正则表达式搜索或替换.

The short answer to your question is that each of these three methods of reading bits of a file have different use cases. As noted above, f.read() reads the file as an individual string, and so allows relatively easy file-wide manipulations, such as a file-wide regex search or substitution.

f.readline() 读取文件的单行,允许用户解析单行而不必读取整个文件.使用 f.readline() 还允许在读取文件时更容易应用逻辑,而不是完整的逐行迭代,例如当文件在中途更改格式时.

f.readline() reads a single line of the file, allowing the user to parse a single line without necessarily reading the entire file. Using f.readline() also allows easier application of logic in reading the file than a complete line by line iteration, such as when a file changes format partway through.

使用语法 for line in f: 允许用户按照问题中的说明逐行迭代文件.

Using the syntax for line in f: allows the user to iterate over the file line by line as noted in the question.

(如其他答案所述,此文档非常值得阅读):

(As noted in the other answer, this documentation is a very good read):

https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects

注意:之前有人声称 f.readline() 可用于在 for 循环迭代期间跳过一行.但是,这在 Python 2.7 中不起作用,并且可能是一种有问题的做法,因此此声明已被删除.

Note: It was previously claimed that f.readline() could be used to skip a line during a for loop iteration. However, this doesn't work in Python 2.7, and is perhaps a questionable practice, so this claim has been removed.

这篇关于我什么时候应该使用 file.read() 或 file.readlines()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆