在我自己的PC上,应用程序运行良好,但当它部署到docker时,由于无效字符而失败.
我正在使用ubuntu:lastest容器python3,java和ruby.
我遇到了Python的问题string.format()并将Unicode字符串传递给它.这与旧的问题类似,只是在我的情况下,测试代码在打印时爆炸,而不是在logging.info()通话中.将相同的Unicode字符串对象传递给日志记录处理程序可以正常工作.
对于较旧的%格式以及此格式,这同样失败string.format().为了确保它是问题的字符串对象,而不是打印与我的终端严重交互,我尝试在打印之前将格式化的字符串分配给变量.
def unicode_test():
byte_string = '\xc3\xb4'
unicode_string = unicode(byte_string, "utf-8")
print "unicode object type: {}".format(type(unicode_string))
output_string = "printed unicode object: {}".format(unicode_string)
print output_string
if __name__ == '__main__':
unicode_test()
Run Code Online (Sandbox Code Playgroud)
字符串对象似乎假设它正在获得ASCII.
% python -V
Python 2.7.2
% python ./unicodetest.py
unicode object type: <type 'unicode'>
Traceback (most recent call last):
File "./unicodetest.py", line 10, in <module>
unicode_test()
File "./unicodetest.py", line 6, in unicode_test
output_string = "printed unicode object: {}".format(unicode_string)
UnicodeEncodeError: 'ascii' codec …Run Code Online (Sandbox Code Playgroud) 如何添加encoding参数logging.basicConfig?
我发现这个错误报告说明现在可以用于Python 3.3.我需要这个用于Python 2.7,并且bug报告说要使用自定义logging.FileHandler对象,但我无法让它工作.