如何修复 Python 中的 unicode/cPickle 错误?

发布于 2024-09-04 01:29:47 字数 103 浏览 9 评论 0原文

ids = cPickle.loads(gem.value)

loads() argument 1 must be string, not unicode
ids = cPickle.loads(gem.value)

loads() argument 1 must be string, not unicode

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

萌能量女王 2024-09-11 01:29:47

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

淑女气质 2024-09-11 01:29:47

cPickle.dumps() 的结果是一个 str 对象,而不是 unicode 对象。您需要在代码中找到解码 pickled str 对象的步骤,并忽略该步骤。

不要尝试将 unicode 对象转换为 str 对象。两个错误并不能构成一个正确。示例(Python 2.6):

>>> import cPickle
>>> ps = cPickle.dumps([1,2,3], -1)
>>> ps
'\x80\x02]q\x01(K\x01K\x02K\x03e.'
>>> ups = ps.decode('latin1')
>>> str(ups)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128)
>>>

您很可能正在使用默认(且效率低下)的协议 0,它会产生“人类可读”的输出:

>>> ps = cPickle.dumps([1,2,3])
>>> ps
'(lp1\nI1\naI2\naI3\na.'
>>>

大概是 ASCII(但没有记录如此),因此 str(gem.value) 拼凑很可能“”“工作”“”:

>>> ps == str(unicode(ps))
True
>>>

The result of cPickle.dumps() is a str object, not a unicode object. You need to find the step in your code where you are decoding the pickled str object, and omit that step.

DON'T try to convert your unicode object to a str object. Two wrongs don't make a right. Example (Python 2.6):

>>> import cPickle
>>> ps = cPickle.dumps([1,2,3], -1)
>>> ps
'\x80\x02]q\x01(K\x01K\x02K\x03e.'
>>> ups = ps.decode('latin1')
>>> str(ups)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128)
>>>

You may well be using the default (and inefficient) Protocol 0 which produces "human readable" output:

>>> ps = cPickle.dumps([1,2,3])
>>> ps
'(lp1\nI1\naI2\naI3\na.'
>>>

which is presumably ASCII (but not documented to be so) so the str(gem.value) kludge may well """work""":

>>> ps == str(unicode(ps))
True
>>>
£冰雨忧蓝° 2024-09-11 01:29:47

您可以通过将 gem.value 设置为字符串而不是 unicode 来修复此问题。

使用str(gem.value)

You can fix it by making gem.value a string, not unicode.

Use str(gem.value)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文