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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38105507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:21:39  来源:igfitidea点击:

When should I ever use file.read() or file.readlines()?

pythoniotimeit

提问by David Refaeli

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

我注意到,如果我遍历一个我打开的文件,在不“读取”它的情况下遍历它会快得多。

i.e.

IE

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

is much faster than

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

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.

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

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

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

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.

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

采纳答案by Checkmate

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.read() 将文件作为单独的字符串读取,因此允许相对简单的文件范围操作,例如文件范围的正则表达式搜索或替换。

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.

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

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

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

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

(如另一个答案中所述,该文档非常好读):

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

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

EDIT: It was previously claimed that 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.

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

EDIT: Added an example of a use case of f.readline() and f.read()

编辑:添加了 f.readline() 和 f.read() 用例的示例

回答by Rudi

Hope this helps!

希望这可以帮助!

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

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

When size is omitted or negative, the entire contents of the file will be read and returned; it's your problem if the file is twice as large as your machine's memory

当 size 省略或为负时,将读取并返回文件的全部内容;如果文件是机器内存的两倍大,那是你的问题

Sorry for all the edits!

对不起所有的编辑!

For reading lines from a file, you can loop over the file object. This is memory efficient, fast, and leads to simple code:

要从文件中读取行,您可以遍历文件对象。这是内存高效,快速,并导致简单的代码:

for line in f:
    print line,

This is the first line of the file.
Second line of the file

回答by Danny

Eesssketit

电子书

That was a brilliant answer. / Something good to know is that wheneever you use the readline() function it reads a line..... and then it won't be able to read it again. You can return to the position by using the seek()function. to go back to the zero position simply type in f.seek(0).

那是一个绝妙的答案。/ 值得一提的是,每当您使用 readline() 函数时,它都会读取一行..... 然后它将无法再次读取。您可以使用该seek()功能返回该位置。要回到零位置,只需输入f.seek(0)

Similiarly, the function f.tell()will let you know at which position you are.

同样,该功能f.tell()将让您知道您在哪个位置。