gpt4 book ai didi

python - 在 Python3 中迭代文件行时 `tell()` 的替代方案?

转载 作者:太空狗 更新时间:2023-10-30 02:26:03 27 4
gpt4 key购买 nike

在 Python3 中遍历文件时如何找到文件光标的位置?

在 Python 2.7 中它很简单,使用 tell()。在 Python3 中,相同的调用会抛出一个 OSError:

Traceback (most recent call last):
File "foo.py", line 113, in check_file
pos = infile.tell()
OSError: telling position disabled by next() call

我的用例是制作读取大型 CSV 文件的进度条。计算总行数太昂贵并且需要额外的传递。近似值非常有用,我不关心缓冲区或其他噪声源,我想知道它是否需要 10 秒或 10 分钟。

重现问题的简单代码。它在 Python 2.7 上按预期工作,但在 Python 3 上抛出:

file_size = os.stat(path).st_size
with open(path, "r") as infile:
reader = csv.reader(infile)
for row in reader:
pos = infile.tell() # OSError: telling position disabled by next() call
print("At byte {} of {}".format(pos, file_size))

这个答案https://stackoverflow.com/a/29641787/321772表明问题是 next() 方法在迭代期间禁用了 tell()。替代方法是逐行手动读取,但该代码位于 CSV 模块中,因此我无法读取。我也无法理解 Python 3 通过禁用 tell() 获得了什么。

那么在 Python 3 中遍历文件行时找出字节偏移量的首选方法是什么?

最佳答案

csv 模块只希望 reader 调用的第一个参数是一个迭代器,它在每次 next 调用时返回一行。所以你可以只使用迭代器包装器而不是计算字符。如果您希望计数准确,则必须以二进制模式打开文件。但事实上,这很好,因为您将没有 csv 模块所期望的行结束转换。

所以一个可能的包装器是:

class SizedReader:
def __init__(self, fd, encoding='utf-8'):
self.fd = fd
self.size = 0
self.encoding = encoding # specify encoding in constructor, with utf8 as default
def __next__(self):
line = next(self.fd)
self.size += len(line)
return line.decode(self.encoding) # returns a decoded line (a true Python 3 string)
def __iter__(self):
return self

您的代码将变为:

file_size = os.stat(path).st_size
with open(path, "rb") as infile:
szrdr = SizedReader(infile)
reader = csv.reader(szrdr)
for row in reader:
pos = szrdr.size # gives position at end of current line
print("At byte {} of {}".format(pos, file_size))

这里的好消息是您保留了 csv 模块的所有功能,包括引用字段中的换行符...

关于python - 在 Python3 中迭代文件行时 `tell()` 的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46405286/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com