[英]Pickle (no file IO): TypeError: a bytes-like object is required, not 'str'
I am getting this TypeError from object serialization alone, ie. 我只是从对象序列化中得到这个TypeError,即。 no file IO involved (unlike other similar posts). 不涉及文件IO(与其他类似帖子不同)。 One piece of the code is: 其中一段代码是:
for itr in range(numiters):
#Sample from proposal distribution
d_star,Jratio,R_star,step = proposal(d_t,R_t,X,Y,alpha)
#Compute the new posterior value, if necessary
a_star = Pickle.dumps(d_star[:R_star+1])
print(type(a_star))
permsdic[a_star] = ...
and another is: 另一个是:
for perm in permsdic.keys():
print(type(perm))
print(perm)
d_t = Pickle.loads(perm)
and yet I get the above error in: d_t = Pickle.loads(perm) The output of print statements in the first block is: 但我在以下错误中得到了上面的错误:d_t = Pickle.loads(perm)第一个块中的打印语句输出为:
class 'bytes' 类“字节”
and yet in 2nd block it is somehow: 但是在第二个区块中却是这样:
class 'str' 'str'类
b'\\x80\\x03]q\\x00(K\\x01K\\x03K\\x05K\\x0cK\\x00e.' b'\\ x80 \\ x03] q \\ x00(K \\ x01K \\ x03K \\ x05K \\ x0cK \\ x00e。'
This is in Python 3.6 (I am trying to adapt an older Python library) 这是Python 3.6中的版本(我正在尝试改编旧的Python库)
I was able to make it work by using eval() function, ie the following works 我能够通过使用eval()函数使其工作,即以下工作
for perm in permsdic.keys():
print(type(perm))
print(perm)
d_t = Pickle.loads(eval(perm))
I still don't understand why this is necessary... 我仍然不明白为什么这是必要的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.