use*_*314 14 python django unicode character-encoding
我通过Django的RSS阅读器项目的工作在这里.
RSS提要将读取类似"OKLAHOMA CITY(AP) - James Harden let"的内容.RSS feed的编码读取encoding ="UTF-8",所以我相信我在下面的代码片段中将utf-8传递给markdown.em dash是它窒息的地方.
我得到Django错误"'ascii'编解码器不能编码位置109中的字符u'\ u2014':ordinal not in range(128)"这是一个UnicodeEncodeError.在传递的变量中,我看到"OKLAHOMA CITY(AP)\ u2014 James Harden".无效的代码行是:
content = content.encode(parsed_feed.encoding, "xmlcharrefreplace")
Run Code Online (Sandbox Code Playgroud)
我正在使用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")
Run Code Online (Sandbox Code Playgroud)
这把问题推到了我的models.py中
def save(self, force_insert=False, force_update=False):
if self.excerpt:
self.excerpt_html = markdown(self.excerpt)
# super save after this
Run Code Online (Sandbox Code Playgroud)
如果我将保存方法更改为...
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)
Run Code Online (Sandbox Code Playgroud)
我得到错误"'ascii'编解码器无法解码位置141的字节0xe2:序号不在范围内(128)"因为现在它读取"\ xe2\x80\x94",其中em dash是
Ian*_*and 12
如果你接收的数据实际上是用UTF-8编码的,那么它应该是一个字节序列 - 一个Python'str'对象,在Python 2.X中
您可以使用断言验证这一点:
assert isinstance(content, str)
Run Code Online (Sandbox Code Playgroud)
一旦你知道那是真的,你就可以转向实际的编码.Python不进行转码 - 例如,直接从UTF-8转换为ASCII.您需要首先通过解码将您的字节序列转换为Unicode字符串:
unicode_content = content.decode('utf-8')
Run Code Online (Sandbox Code Playgroud)
(如果您可以信任parsed_feed.encoding,那么请使用它而不是文字'utf-8'.无论哪种方式,都要为错误做好准备.)
然后,您可以获取该字符串,并使用ASCII对其进行编码,将高字符替换为其实体等效项:
xml_content = unicode_content.encode('ascii', 'xmlcharrefreplace')
Run Code Online (Sandbox Code Playgroud)
那么完整的方法看起来像这样:
try:
content = content.decode(parsed_feed.encoding).encode('ascii', 'xmlcharrefreplace')
except UnicodeDecodeError:
# Couldn't decode the incoming string -- possibly not encoded in utf-8
# Do something here to report the error
Run Code Online (Sandbox Code Playgroud)