简体   繁体   English

泡菜(无文件IO):TypeError:需要一个类似字节的对象,而不是“ str”

[英]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.

相关问题 需要一个类似字节的对象,而不是'str':压缩文件中的typeerror - a bytes-like object is required, not 'str': typeerror in compressed file TypeError:需要一个类似字节的对象,而不是“ str”,但类型是“ bytes” - TypeError: a bytes-like object is required, not 'str' but type is 'bytes' 是什么导致此“类型错误:需要类似字节的 object,而不是‘str’”错误? - What is causing this "TypeError: a bytes-like object is required, not 'str'" error? Python TypeError-必需的类似字节的对象,而不是str - Python TypeError - required bytes-like object instead of str TypeError 需要一个类似字节的对象,而不是'str'(Pydub) - TypeError a bytes-like object is required, not 'str' (Pydub) TypeError:需要一个类似字节的对象,对于Geniatagger不是'str'吗? - TypeError: a bytes-like object is required, not 'str' for Geniatagger? 类型错误:需要类似字节的 object,而不是使用子进程 python 的“str” - TypeError: a bytes-like object is required, not 'str' using Subprocess python Python 错误类型错误:需要类似字节的 object,而不是“str” - Python ERROR TypeError: a bytes-like object is required, not 'str' TypeError:从 .gz 文件中拆分行时需要类似字节的对象,而不是“str” - TypeError: a bytes-like object is required, not 'str' when splitting lines from .gz file TypeError:需要类似字节的 object,而不是“KeyCode” - TypeError: a bytes-like object is required, not 'KeyCode'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM