什么可能导致 UnicodeEncodeError 异常潜入正在运行的 Python 环境中?
我的脚本中有一个方法,可以提取 Twitter RSS 提要,使用 FeedPharser 对其进行解析,使用 twilio 模块将其包装在 TwiML(Twilio 风格的 XML)中,并通过 str() 在 CherryPy 方法中返回结果响应。这在开发环境(Kubuntu 10.10)中工作得很好;我的服务器(Linode 上的 Ubuntu Server 10.10)上的结果好坏参半。
最初的几个月,一切都很好。然后,上述方法开始失败,如下所示:
UnicodeEncodeError:“ascii”编解码器 无法对字符 u'\u2019' 进行编码 位置 259:序号不在 范围(128)
但是,当我在相同的 feed、相同的 python 版本、相同的操作系统、我的开发盒上运行完全相同的代码时,代码执行得很好。但是,我应该注意到,即使它工作正常,某些字符也无法正确输出。例如:
而不是
'
只是从头开始重建我的 VPS,这又工作了几个月,然后错误又回来了。
服务器自动安装更新的 Ubuntu 软件包,但我的开发盒也是如此。我想不出有什么可能导致这种情况。任何帮助表示赞赏。
I have a method in my script that pulls a Twitter RSS feed, parses it with FeedPharser, wraps it in TwiML (Twilio-flavored XML) using the twilio module, and returns the resulting response in a CherryPy method via str(). This works my fine in development environment (Kubuntu 10.10); I have had mixed results on my server (Ubuntu Server 10.10 on Linode).
For the first few months, all was well. Then, the method described above began to fail with something like:
UnicodeEncodeError: 'ascii' codec
can't encode character u'\u2019' in
position 259: ordinal not in
range(128)
But, when I run the exact same code on the same feed, with the same python version, on the the same OS, on my development box, the code executes fine. However, I should note that even when it does work properly, some characters aren't outputted right. For example:
’
rather than
'
To solve this anomaly, I simply rebuilt my VPS from scratch, which worked for a few more months, and then the error came back.
The server automatically installs updated Ubuntu packages, but so does my development box. I can't think of anything that could cause this. Any help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XML 数据不能包含某些字符。一个简单的解决方法是将数据包装在 XML 标记内,该标记会向您提供 CDATA 错误。例如:
或者您可以使用数字参考值 ex
&
表示 &有关详细信息,请访问:
http://en.wikipedia.org/wiki/XML#Characters_and_escaping
http://en.wikipedia.org/wiki/Numeric_character_reference
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
http://en.wikipedia.org/wiki/CDATA
XML data cannot contain certain characters. An easy workaround is to wrap the data inside your XML tag that is giving you the error with CDATA. For example:
Or you can use numerical reference values ex
&
for &More information on this is available here:
http://en.wikipedia.org/wiki/XML#Characters_and_escaping
http://en.wikipedia.org/wiki/Numeric_character_reference
http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
http://en.wikipedia.org/wiki/CDATA
几次重新启动后(由于不相关的原因),它又可以工作了。多么奇怪啊……
A few reboots later (for unrelated reasons) and it's working again. How odd....