【发布时间】:2013-02-20 20:40:00
【问题描述】:
为了记录 utf-8 字符串,我是否需要手动记录 print 为我所做的事情?
for line in unicodecsv.reader(cfile, encoding="utf-8"):
for i in line:
print "process_clusters: From CSV: %s" % i
print "repr: %s" % repr(i)
log.debug("process_clusters: From CSV: %s", i)
无论字符串是拉丁文还是俄文西里尔文,我的打印语句都能正常工作。
process_clusters: From CSV: escuchan
repr: u'escuchan'
process_clusters: From CSV: говоритъ
repr: u'\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044a'
但是,log.debug 不会让我传入相同的变量。我收到此错误:
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/logging/__init__.py", line 765, in emit
self.stream.write(fs % msg.encode("UTF-8"))
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/codecs.py", line 686, in write
return self.writer.write(data)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/codecs.py", line 351, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 28: ordinal not in range(128)
我的日志、格式化程序和处理程序是:
log = logging.getLogger(__name__)
loglvl = getattr(logging, loglevel.upper()) # convert text log level to numeric
log.setLevel(loglvl) # set log level
handler = logging.FileHandler('inflection_finder.log', 'w', 'utf-8')
handler.setFormatter(logging.Formatter('[%(levelname)s] %(message)s'))
log.addHandler(handler)
我使用的是 Python 2.6.7。
【问题讨论】:
-
您还应该显示
log对象的创建以及添加到它的任何处理程序。很可能,问题就在这里。 -
我认为这个问题可能是 Python2.6.x 特有的。我没有设法重现问题in Python2.7。
-
@unutbu:我也无法在 2.6 上重现它。
-
添加了处理程序信息和字符串的 repr 以获得更好的可见性。
-
@unutbu,我希望你是对的。我安装了 2.7,但仍然可以始终如一地重现该问题。