简体   繁体   English

迭代文件python中的行

[英]Iterating over lines in a file python

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?我知道在第一种情况下,该文件将充当一个列表,因此我们在迭代列表元素时迭代一个文件,但在第二种情况下,我不确定如何解释读取文件时会发生什么首先,然后迭代它?

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 at once;在这种情况下,整个文件数据不会一次读入内存; instead, only the current line is read into memory.相反,只有当前行被读入内存。 This is useful for handling very large files, and good for robustness if you don't know if the file is going to be large or not.这对于处理非常大的文件很有用,并且如果您不知道文件是否会很大,则有利于稳健性。

In the second one, file.read() returns the complete file data as a string.在第二个中, file.read()以字符串形式返回完整的文件数据。 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.这会将完整的文件数据读入内存。

Here's an 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

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()将内容作为行列表read()

with open('file.txt','r') as fin:
    lines = fin.readlines()
for line in lines:
    # do something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM