繁体   English   中英

AttributeError文件对象没有属性“原始”错误

[英]AttributeError File object has no attribute 'raw' Error

我有以下由Michael编写的用于廉价查找行数的代码,但是当我运行它时,由于AttributeError File object has no attribute 'raw'错误,这给了我一个错误,我不确定为什么会这样。 以下是供参考的代码,非常感谢您的帮助

from itertools import (takewhile,repeat)

def _make_gen(reader):
    b = reader(1024 * 1024)
    while b:
        yield b
        b = reader(1024*1024)

def rawpycount(filename):
    f = open(filename, 'rb')
    f_gen = _make_gen(f.raw.read)
    return sum( buf.count(b'\n') for buf in f_gen )

_make_gen(f.raw.read)更改为_make_gen(f.read)

Python 3.x默认情况下使用unicode,因此将raw转换为字节。 另一方面,Python 2.x默认使用字节,因此不需要其他任何东西。

暂无
暂无

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

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