我正在使用导入了__future__
库的Python2进行编写。
当我想按以下方式记录unicode字符串时,
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.info("╔")
我收到此错误。
Traceback (most recent call last):
File "/usr/local/gcc-5-glibc-2.23/lib/python2.7/logging/__init__.py", line 876, in emit
stream.write(fs % msg.encode("UTF-8"))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 231: ordinal not in range(128)
答案 0 :(得分:0)
我有一个临时的丑陋修复程序。但是,为什么呢?还有更好的解决方法吗?
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import logging
import sys
reload(sys) # noqa
sys.setdefaultencoding("utf8")
logging.basicConfig()
logger = logging.getLogger(__name__)
logger.info("╔")