UnicodeEncodeError:'charmap'编解码器无法编码字符

时间:2012-03-09 04:07:02

标签: python

Python在使用wolfram alpha api时会抛出这个:

Traceback (most recent call last):
  File "c:\Python27\lib\threading.py", line 530, in __bootstrap_inner
    self.run()
  File "c:\Python27\lib\site-packages\Skype4Py\utils.py", line 225, in run
    handler(*self.args, **self.kwargs)
  File "s.py", line 38, in OnMessageStatus
    if body[0:5] == '!math':wolfram(body[5:], '')
  File "s.py", line 18, in wolfram
    print "l: "+l
  File "c:\Python27\lib\encodings\cp437.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\xd7' in position 3
: character maps to <undefined>

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:6)

看起来你正在将高字节数据传递给API,并且它不喜欢它(\ xd7是“Times”字符;看起来像X)。我不确定该打印的目的是什么,但将其更改为print "l: " + repr(l)print "l: ", l可能至少会让您超过上述错误,假设您不想参与将身体转换为unicode(我假设它不是......)。

如果这没有帮助,我们需要更多细节。你的意见来自哪里?是身体unicode,还是字节串?你使用的是python 2.7还是3.x?