【发布时间】:2011-01-31 13:34:06
【问题描述】:
我正在研究 Django RSS 阅读器项目here。
RSS 提要将显示类似“OKLAHOMA CITY (AP) — James Harden let”的内容。 RSS 提要的编码读取 encoding="UTF-8" 所以我相信我在下面的代码 sn-p 中将 utf-8 传递给 markdown。破折号是它窒息的地方。
我收到 Django 错误“'ascii'编解码器无法在位置 109 编码字符 u'\u2014':序数不在范围 (128)”,这是一个 UnicodeEncodeError。在传递的变量中,我看到“OKLAHOMA CITY (AP) \u2014 James Harden”。不工作的代码行是:
content = content.encode(parsed_feed.encoding, "xmlcharrefreplace")
我正在使用 markdown 2.0、django 1.1 和 python 2.4。
要完成这项工作,我需要执行什么神奇的编码和解码序列?
(应普罗米修斯的要求。我同意格式有帮助)
所以在视图中,我在 parsed_feed 编码行上方添加了 smart_unicode 行...
content = smart_unicode(content, encoding='utf-8', strings_only=False, errors='strict')
content = content = content.encode(parsed_feed.encoding, "xmlcharrefreplace")
这将问题推到了我的models.py 中
def save(self, force_insert=False, force_update=False):
if self.excerpt:
self.excerpt_html = markdown(self.excerpt)
# super save after this
如果我将保存方法更改为...
def save(self, force_insert=False, force_update=False):
if self.excerpt:
encoded_excerpt_html = (self.excerpt).encode('utf-8')
self.excerpt_html = markdown(encoded_excerpt_html)
我收到错误 "'ascii' codec can't decode byte 0xe2 in position 141: ordinal not in range(128)" 因为现在它读取 "\xe2\x80\x94" 其中破折号是
【问题讨论】:
-
能否按原样发布回溯?
-
基本上
parsed_feed.encoding的值是多少?它是'ascii',有机会吗? (这可以解释你的两个错误)。
标签: python django unicode character-encoding