简体   繁体   English

TypeError:文件必须具有在 Python3 中运行的 'read' 和 'readline' 属性

[英]TypeError: file must have 'read' and 'readline' attributes run in Python3

I have searched about this type of errors, tried to fix as much as I can but still, this error hasn't gone away我已经搜索过这种类型的错误,尝试尽可能多地修复,但是这个错误并没有消失

Traceback (most recent call last):
  File "rbm.py", line 419, in <module>
    learn_letters()
  File "rbm.py", line 350, in learn_letters
    import rbm
  File "/Users/thienhua/Desktop/usingnow/coding/sachml/Ch17/rbm.py", line 423, in <module>
    test_rbm()
  File "/Users/thienhua/Desktop/usingnow/coding/sachml/Ch17/rbm.py", line 259, in test_rbm
    train_set = pickle.load(file)
TypeError: file must have 'read' and 'readline' attributes

this is the line code where I got stuck:这是我卡住的行代码:

def test_rbm():
   import rbm
   import pickle

   # Load the dataset
   with open('mnist.pkl', 'rb') as f:
      file = pickle._Unpickler(f)
      file.encoding = 'latin1'
      train_set = pickle.load(file)
      valid_set = pickle.load(file)
      test_set = pickle.load(file)
      #train_set, valid_set, test_set = pickle.load(file)
      file.close()

   r = rbm.rbm(28*28,100,inputs = train_set[0][:100,:],momentum=0.4,nCDsteps=3,nepochs=5000)
   r.contrastive_divergence(train_set[0][:100,:])

Im using python 3.8 at the moment.我目前使用 python 3.8。 Thank you so much for your help.非常感谢你的帮助。

The issue is that pickle.load accepts file-like objects, but pickle._Unpickler() doesn't return that kind of object (it returns instance of class _Unpickler , which doesn't have 'read' and 'readline' attrs).问题是pickle.load接受类似文件的对象,但pickle._Unpickler()不返回那种 object (它返回class _Unpickler的实例,它没有“读取”和“读取线”属性)。 If you have pickled python object into some file, you have to load it back through pickle.load('/path/to/pickle_file') .如果您已将 python object 腌制到某个文件中,则必须通过pickle.load('/path/to/pickle_file')将其加载回来。 More info on https://docs.python.org/3/library/pickle.html有关https://docs.python.org/3/library/pickle.html的更多信息

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

相关问题 TypeError:文件必须具有“read”和“readline”属性 - TypeError: file must have 'read' and 'readline' attributes 使用readline读取txt文件python3 - Use readline to read txt file python3 Python 文件 read() 和 readline() 计数器? - Python file read() and readline() counter? TypeError:字符串索引必须是整数-Python3 - TypeError: string indices must be integers - Python3 Python3 我一直在犯这个错误 [TypeError: list indices must be integers or slices, not NoneType] - Python3 i have been geting this errer [TypeError: list indices must be integers or slices, not NoneType] TypeError:字符串索引必须是整数,同时使用 python3 读取 json 文件 - TypeError: string indices must be integers, while reading a json file with python3 使用 readline() 读取整行,然后使用 Python 移动到下一个 in.txt 文件,每次后续运行 - Using readline() to read a full line then move onto the next one in .txt file with each subsequent run using Python ElementTree TypeError“write()参数必须是str,而不是Python3中的字节” - ElementTree TypeError “write() argument must be str, not bytes” in Python3 Python3:TypeError:列表索引必须是整数或切片,而不是str - Python3: TypeError: list indices must be integers or slices, not str 在 Python3 中解码字节字符串时出错 [TypeError: must be str, not bytes] - Error decoding byte string in Python3 [TypeError: must be str, not bytes]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM