7

我有脚本可以通过日志系统打印出消息,或者有时打印命令。在 Windows 控制台上,我收到错误消息,例如

Traceback (most recent call last):
  File "C:\Python32\lib\logging\__init__.py", line 939, in emit
    stream.write(msg)
  File "C:\Python32\lib\encodings\cp850.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position 4537:character maps to <undefined>

是否有一种通用方法可以使日志系统、打印命令等中的所有编码都具有故障安全(忽略错误)?

4

1 回答 1

9

问题是您的终端/shell(Windows 上的 cmd)无法打印每个 Unicode 字符。

您可以使用方法的errors参数对字符串进行故障安全编码str.encode。例如,您可以?通过设置替换不支持的字符errors='replace'

>>> s = u'\u2019'
>>> print s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\encodings\cp850.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can\'t encode character u'\u2019' in position
 0: character maps to <undefined>
>>> print s.encode('cp850', errors='replace')
?

有关其他选项,请参阅文档

编辑如果您想要一个通用的日志记录解决方案,您可以子类化StreamHandler

class CustomStreamHandler(logging.StreamHandler):

    def emit(self, record):
        record = record.encode('cp850', errors='replace')
        logging.StreamHandler.emit(self, record)
于 2012-06-15T12:22:00.563 回答