为了记录 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。