TypeError:需要一个类似字节的对象,而不是在用pickle加载时为"str" [英] TypeError: a bytes-like object is required, not 'str' while loading with pickle

查看:62
本文介绍了TypeError:需要一个类似字节的对象,而不是在用pickle加载时为"str"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python 3.6和Spyder(Anaconda).我尝试了很多事情,但没有解决.我不知道为什么在加载泡菜时总是会出现此错误.

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'

推荐答案

您需要将文件作为二进制文件打开:

You need to open your file as a binary file:

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

否则,它使用 str 读取数据.

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

这篇关于TypeError:需要一个类似字节的对象,而不是在用pickle加载时为"str"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆