if type(value) == str:
# Ignore errors even if the string is not proper UTF-8 or has
# broken marker bytes.
# Python built-in function unicode() can do this.
value = unicode(value, "utf-8", errors="ignore")
else:
# Assume the value object has proper __unicode__() method
value = unicode(value)
很可能,您的问题是解析得很好,而现在您正试图打印XML的内容,但由于存在一些外来的Unicode字符,因此无法打印。首先尝试将unicode字符串编码为ascii:
“忽略”部分会告诉它跳过那些字符。从python文档:
你可能想读一读这篇文章:http://www.joelonsoftware.com/articles/Unicode.html,我发现它作为一个关于正在发生的事情的基本教程非常有用。阅读完之后,你就会不再觉得自己只是在猜测要使用什么命令(或者至少是发生在我身上的情况)。
不要在脚本中硬编码环境的字符编码;请直接打印Unicode文本:
如果输出被重定向到文件(或管道);可以使用
PYTHONIOENCODING
envvar指定字符编码:否则,
python your_script.py
应该按原样工作——您的语言环境设置用于对文本进行编码(在POSIX检查时:LC_ALL
,LC_CTYPE
,LANG
环境变量——如有必要,将LANG
设置为utf-8语言环境)。To print Unicode on Windows, see this answer that shows how to print Unicode to Windows console, to a file, or using IDLE。
更好的解决方案:
如果您想了解更多有关原因的信息:
http://docs.plone.org/manage/troubleshooting/unicode.html#id1
相关问题 更多 >
编程相关推荐