简体   繁体   English

python修改可变迭代器

[英]python modify mutable iterator

The code is like following: 代码如下:

f=open('test.txt')
file=iter(f)

When I do 当我做

next(file)

It will print line by line of file. 它将逐行打印文件。 But when I modified the test.txt file and saved it, the next(file) still printed the original file content. 但是当我修改test.txt文件并保存它时,下一个(文件)仍然打印了原始文件内容。

Does iterator store the complete file in the memory? 迭代器是否将完整文件存储在内存中? If not why the content of the file didn't get updated? 如果不是为什么文件的内容没有得到更新?

No, as an iterator the file object stores only a look-ahead buffer, not the complete file, in memory. 不,作为迭代器, file对象仅在内存中存储前瞻缓冲区而不是完整文件。 This makes it efficient for large files. 这使得它对大文件有效。

Since there is this look-ahead buffer, changes made to the file won't be reflected to the next method. 由于存在此预见缓冲区,因此对文件所做的更改不会反映到next方法。 However, you can use the seek method to clear this buffer so the next call to the next method will return the updated content: 但是,您可以使用seek方法清除此缓冲区,以便下一次调用next方法时将返回更新的内容:

f.seek(f.tell()) # seek the current position only to clear the look-ahead buffer
print(next(f)) # prints the updated next line from the current position

Let's suppose open() reads 2 letters at a time. 我们假设open()一次读取2个字母。 (Actual value is io.DEFAULT_BUFFER_SIZE ) (实际值为io.DEFAULT_BUFFER_SIZE

f=open('test.txt')

You've created a file object, _io.TextIOWrapper which, oversimplified, is like [{read from 0 to io.DEFAULT_BUFFER_SIZE of test.txt}, ...} 你已经创建了一个文件对象_io.TextIOWrapper ,它过于简单,就像[{read from 0 to io.DEFAULT_BUFFER_SIZE of test.txt}, ...}

file=iter(f)

You've created a iterator of _io.TextIOWrapper with data like this: [{read from 0 to 1}, ... {read from n-1 to n}] 您已经使用以下数据创建了_io.TextIOWrapper的迭代器: [{read from 0 to 1}, ... {read from n-1 to n}]

next(file)

next() has went through the first item of file , read it, and printed it. next()已经浏览了第一个file ,读取并打印出来。

Let's learn from an example. 让我们从一个例子中学习。

Reading normally 正常阅读

test.txt 的test.txt

what a beautiful day

We will open the file, iter(), and list() to open and iter through all of it and make a list. 我们将打开文件iter()和list()以打开并通过所有文件创建一个列表。

In [1]: f = open('test.txt')

In [2]: list(iter(f))
Out[2]: ['what a beautiful day']

Just as expected. 正如预期的那样。

File changes after open() open()后文件更改

In [1]: f = open('test.txt')

We've opened the file. 我们已经打开了这个文件。

We will now append hello open() to test.txt. 我们现在将hello open()追加到test.txt。

test.txt 的test.txt

what a beautiful day

hello open()

and then iter() and list() it. 然后是iter()和list()它。

In [2]: list(iter(f))
Out[2]: ['what a beautiful day\n', '\n', 'hello open()']

The changed contents are seen. 看到改变的内容。 We can see that open() does not actually read the file. 我们可以看到open()实际上并没有读取文件。

File changes after iter() iter()之后的文件更改

In [1]: f = open('test.txt')

In [2]: i = iter(f)

We've opened the file and iter() d. 我们打开了文件和iter() d。

We will now append hello iter() 我们现在将追加hello iter()

test.txt 的test.txt

what a beautiful day

hello open()

hello iter()

and then list() it. 然后列出()它。

In [3]: list(i)
Out[3]: ['what a beautiful day\n', '\n', 'hello open()\n', '\n', 'hello iter()']

The changed contents are seen. 看到改变的内容。 We can also see that iter() does not actually read the file. 我们还可以看到iter()实际上并没有读取文件。

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

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