本文介绍了如何解决Python中的unicode/cPickle错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ids = cPickle.loads(gem.value)

loads() argument 1 must be string, not unicode

推荐答案

cPickle.loads想要一个字节字符串(正是cPickle.dumps输出的),而您却向它提供了一个unicode字符串.您需要对该Unicode字符串进行编码",以获取dumps最初给您的字节字符串,但是很难猜测您不小心对它施加了什么编码-可能是latin-1utf-8(如果ascii不用担心,这两个都可以很好地解码),也许utf-16 ...?不知道gem是什么,以及如何根据cPickle.dumps ...!

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...!

这篇关于如何解决Python中的unicode/cPickle错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:02