在Python3中迭代文件行时`tell()`的替代方法? |
您所在的位置:网站首页 › pythontell方法 › 在Python3中迭代文件行时`tell()`的替代方法? |
在迭代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))这个答案/sf/ask/17360801/表明问题是该next()方法tell()在迭代期间禁用.替代方案是逐行手动读取,但该代码在CSV模块内,所以我无法得到它.我也无法通过禁用来理解Python 3的收益tell(). 那么在迭代Python 3中的文件行时找出字节偏移量的首选方法是什么? 1> Serge Balles..: 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模块的所有功能,包括引用字段中的换行符...... |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |