【问题标题】:Python: rewinding one line in file when iterating with f.next()Python:使用 f.next() 迭代时倒回文件中的一行
【发布时间】:2023-03-23 00:09:01
【问题描述】:

当您使用 f.next() 遍历文件时,Python 的 f.tell 无法正常工作:

>>> f=open(".bash_profile", "r")
>>> f.tell()
0
>>> f.next()
"alias rm='rm -i'\n"
>>> f.tell()
397
>>> f.next()
"alias cp='cp -i'\n"
>>> f.tell()
397
>>> f.next()
"alias mv='mv -i'\n"
>>> f.tell()
397

看起来它为您提供了缓冲区的位置,而不是您刚刚使用 next() 获得的位置。

我之前使用了 seek/tell trick 在使用 readline() 遍历文件时回退一行。使用 next() 时有没有办法倒回一行?

【问题讨论】:

    标签: python next seek


    【解决方案1】:

    没有。我会制作一个适配器,在很大程度上转发所有呼叫,但在您执行next 时保留最后一行的副本,然后让您调用不同的方法以使该行再次弹出。

    我实际上会让适配器成为一个可以包装任何可迭代而不是文件包装器的适配器,因为这听起来在其他上下文中经常有用。

    Alex 关于使用itertools.tee 适配器的建议也有效,但我认为编写自己的迭代器适配器来处理这种情况通常会更简洁。

    这是一个例子:

    class rewindable_iterator(object):
        not_started = object()
    
        def __init__(self, iterator):
            self._iter = iter(iterator)
            self._use_save = False
            self._save = self.not_started
    
        def __iter__(self):
            return self
    
        def next(self):
            if self._use_save:
                self._use_save = False
            else:
                self._save = self._iter.next()
            return self._save
    
        def backup(self):
            if self._use_save:
                raise RuntimeError("Tried to backup more than one step.")
            elif self._save is self.not_started:
                raise RuntimeError("Can't backup past the beginning.")
            self._use_save = True
    
    
    fiter = rewindable_iterator(file('file.txt', 'r'))
    for line in fiter:
        result = process_line(line)
        if result is DoOver:
            fiter.backup()
    

    这不会太难扩展到允许您备份多个值的东西。

    【讨论】:

    • 这对我来说是最好的解决方案。我已经有了类似包装器的东西,所以用这种方式修改它很容易。
    • python3 的更新:使用__next__ 代替下一个,这个例子就会成功。见getpython3.com/diveintopython3/…
    【解决方案2】:

    Python 的文件迭代器会进行大量缓冲,从而将文件中的位置提前到迭代之前。如果您想使用file.tell(),您必须使用“旧方式”:

    with open(filename) as fileob:
      line = fileob.readline()
      while line:
        print fileob.tell()
        line = fileob.readline()
    

    【讨论】:

      【解决方案3】:

      itertools.tee 可能是最不坏的方法——你不能“击败”通过迭代文件完成的缓冲(你也不希望:性能影响会很糟糕),所以保留两个迭代器,一个“落后”另一个,对我来说似乎是最合理的解决方案。

      import itertools as it
      
      with open('a.txt') as f:
        f1, f2 = it.tee(f)
        f2 = it.chain([None], f2)
        for thisline, prevline in it.izip(f1, f2):
          ...
      

      【讨论】:

        猜你喜欢
        • 2016-02-10
        • 2018-05-24
        • 1970-01-01
        • 2011-02-16
        • 1970-01-01
        • 2021-09-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多