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

查看:174
本文介绍了我什么时候应该使用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.

使用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/2/tutorial/inputoutput.html#methods-of-file-objects

编辑:
之前声称readline()可用于在for循环迭代期间跳过一行。但是,这在python 2.7中不起作用,可能是一个值得怀疑的做法,因此该声明已被删除。

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.

编辑:
添加了一个示例f.readline()和f.read()

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

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

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