TypeError: a bytes-like object is required, not 'str' while loading with pickle

Deadly 提交于 2020-07-16 09:12:07

问题


I'm using Python 3.6 and Spyder (Anaconda). I have tried many things but nothing worked out. I don't know why this error is coming always to me while loading with pickle.

filename = "allfeatures.txt"
allfeatures = open(filename, 'r').read()
with open(filename) as f:
     allfeatures = list(f)
allconcat = np.vstack(list(allfeatures.values()))
TypeError                           Traceback (most recent call last)
 AttributeError: 'list' object has no attribute 'values'

回答1:


You need to open your file as a binary file:

pickle.loads(open("accounts.txt", 'rb').read())

Otherwise, it's using an str to read the data.



来源:https://stackoverflow.com/questions/53052551/typeerror-a-bytes-like-object-is-required-not-str-while-loading-with-pickle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!