迭代文件python中的行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31671685/
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
Iterating over lines in a file python
提问by Homap
I am learning python. Would it be possible if someone could explain the different between the following for processing a file:
我正在学习蟒蛇。如果有人可以解释以下处理文件的不同之处,是否有可能:
file = open("file.txt")
for line in file:
#do something
file = open("file.txt")
contents = file.read()
for line in contents:
# do something
I know that in the first case, the file will act as a list so we iterate over a file as we iterate over the elements of a list but in the second case, I am not sure how to explain what happens if I read the file first and then iterate over it?
我知道在第一种情况下,该文件将充当一个列表,因此我们在迭代列表的元素时迭代一个文件,但在第二种情况下,我不确定如何解释读取文件时会发生什么首先,然后迭代它?
采纳答案by Anand S Kumar
In the first one you are iterating over the file , line by line. In this scenario, the entire file data is not read into the memory, instead only the current line is read into memory , this is useful for handling very large files.
在第一个中,您正在逐行迭代文件。在这种情况下,不会将整个文件数据读入内存,而是仅将当前行读入内存,这对于处理非常大的文件很有用。
In the second one, file.read()
returns the complete file data as a string, when you are iterating over it , you are actually iterating over the file's data character by character. This reads the complete file data into memory.
在第二个中,file.read()
将完整的文件数据作为字符串返回,当您对其进行迭代时,您实际上是在逐个字符地迭代文件的数据。这会将完整的文件数据读入内存。
Example to show this behavior -
显示此行为的示例 -
a.txt file contains -
a.txt 文件包含 -
Hello
Bye
Code
代码
>>> f = open('a.txt','r')
>>> for l in f:
... print(l)
...
Hello
Bye
>>> f = open('a.txt','r')
>>> r = f.read()
>>> print(repr(r))
'Hello\nBye'
>>> for c in r:
... print(c)
...
H
e
l
l
o
B
y
e
回答by khelwood
The second case reads in the contents of the file into one big string. If you iterate over a string, you get each character in turn. If you want to get each line in turn, you can do this:
第二种情况将文件的内容读入一个大字符串。如果你遍历一个字符串,你会依次得到每个字符。如果你想依次得到每一行,你可以这样做:
for line in contents.split('\n'):
# do something
Or you can read in the contents as a list of lines using readlines()
instead of read()
.
或者,您可以使用readlines()
代替将内容作为行列表读入read()
。
with open('file.txt','r') as fin:
lines = fin.readlines()
for line in lines:
# do something