How do I fix this unicode/cPickle error in Python?

后端 未结 3 353
無奈伤痛
無奈伤痛 2021-01-24 09:13
ids = cPickle.loads(gem.value)

loads() argument 1 must be string, not unicode
3条回答
  •  [愿得一人]
    2021-01-24 09:59

    cPickle.loads wants a byte string (which is exactly what cPickle.dumps outputs) and you're feeding it a unicode string instead. You'll need to "encode" that Unicode string to get back the byte string that dumps had originally given you, but it's hard to guess what encoding you accidentally imposed on it -- maybe latin-1 or utf-8 (if ascii don't worry, either of those two will decode it just great), maybe utf-16...? It's hard to guess without knowing what gem is and how you originally set its value from the output of a cPickle.dumps...!

提交回复
热议问题