as3:~/ngokevin-site# nano content/blog/20140114_test-chinese.mkd
as3:~/ngokevin-site# wok
Traceback (most recent call last):
  File "/usr/local/bin/wok", line 4, in
    Engine()
  File "/usr/local/lib/python2.7/site-packages/wok/engine.py", line 104, in init
    self.load_pages()
  File "/usr/local/lib/python2.7/site-packages/wok/engine.py", line 238, in load_pages
    p = Page.from_file(os.path.join(root, f), self.options, self, renderer)
  File "/usr/local/lib/python2.7/site-packages/wok/page.py", line 111, in from_file
    page.meta['content'] = page.renderer.render(page.original)
  File "/usr/local/lib/python2.7/site-packages/wok/renderers.py", line 46, in render
    return markdown(plain, Markdown.plugins)
  File "/usr/local/lib/python2.7/site-packages/markdown/init.py", line 419, in markdown
    return md.convert(text)
  File "/usr/local/lib/python2.7/site-packages/markdown/init.py", line 281, in convert
    source = unicode(source)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe8 in position 1: ordinal not in range(128). -- Note: Markdown only accepts unicode input!

如何解决?

在其他一些基于python的静态博客应用中,中文帖子可以成功发布。 比如这个应用:http://github.com/vrypan/bucket3。在我的网站http://bc3.brite.biz/,中文帖子可以成功发布。


当前回答

简而言之,为了确保在Python 2中正确处理unicode:

使用io。打开文件读写 使用from __future__ import unicode_literals 配置其他数据输入/输出(例如,数据库,网络)使用unicode 如果不能将输出配置为utf-8,请将输出转换为print(text。编码(“ascii”、“替换”).decode ())

有关解释,请参阅@Alastair McCormack的详细回答。

其他回答

简而言之,为了确保在Python 2中正确处理unicode:

使用io。打开文件读写 使用from __future__ import unicode_literals 配置其他数据输入/输出(例如,数据库,网络)使用unicode 如果不能将输出配置为utf-8,请将输出转换为print(text。编码(“ascii”、“替换”).decode ())

有关解释,请参阅@Alastair McCormack的详细回答。

这招对我很管用:

    file = open('docs/my_messy_doc.pdf', 'rb')

在Django (1.9.10)/Python 2.7.5项目中,我经常出现UnicodeDecodeError异常;主要是当我试图向日志记录提供unicode字符串时。我为任意对象创建了一个辅助函数,基本上格式化为8位ascii字符串,并将表中不包含的任何字符替换为'?'。我认为这不是最好的解决方案,但由于默认编码是ascii(我不想改变它),它会这样做:

encode_for_logging(c, encoding='ascii'): 如果isinstance(c, basestring): 返回c.encode(encoding, 'replace') elif isinstance(c, Iterable): C_ = [] 对于v (c) c_。追加(encode_for_logging (v,编码) 返回c_ 其他: 返回encode_for_logging (unicode (c)) `

我得到了字符串“PastelerÃ-a Mallorca”同样的问题,我用:

unicode("Pastelería Mallorca", 'latin-1')

这是典型的“统一码问题”。我相信,解释这个问题已经超出了StackOverflow回答的范围,无法完全解释正在发生的事情。

这里有很好的解释。

简单地说,您已经将一个被解释为字节字符串的内容传递给了需要将其解码为Unicode字符的内容,但是默认的编解码器(ascii)失败了。

我给你们看的演示提供了避免这种情况的建议。让你的代码成为“unicode三明治”。在Python 2中,使用from __future__ import unicode_literals会有所帮助。

更新:如何修复代码:

OK - in your variable "source" you have some bytes. It is not clear from your question how they got in there - maybe you read them from a web form? In any case, they are not encoded with ascii, but python is trying to convert them to unicode assuming that they are. You need to explicitly tell it what the encoding is. This means that you need to know what the encoding is! That is not always easy, and it depends entirely on where this string came from. You could experiment with some common encodings - for example UTF-8. You tell unicode() the encoding as a second parameter:

source = unicode(source, 'utf-8')