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 3。我遵循了这个方法,解决了我的问题:

enc = sys.getdefaultencoding()
file = open(menu, "r", encoding = enc)

在读取/写入文件时,必须设置编码。

其他回答

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

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

我在Python2.7中遇到了这个错误。我在尝试运行许多python程序时遇到了这种情况,但我设法用这个简单的脚本重现了它:

#!/usr/bin/env python

import subprocess
import sys

result = subprocess.Popen([u'svn', u'info'])
if not callable(getattr(result, "__enter__", None)) and not callable(getattr(result, "__exit__", None)):
    print("foo")
print("bar")

在成功的情况下,它应该打印出'foo'和'bar',如果你不在svn文件夹中,可能会有一个错误消息。

在失败时,它应该打印'UnicodeDecodeError: 'ascii' codec不能解码字节0xc4在位置39:序号不在范围(128)'。

在尝试重新生成区域设置和这个问题中发布的许多其他解决方案后,我了解到发生了错误,因为我的PATH环境变量中编码了一个特殊字符(ĺ)。在` ~/中固定PATH后。Bashrc ',然后退出我的会话并再次进入,(显然是在查找'~/。Bashrc’没有起作用),问题就消失了。

在某些情况下,当你检查你的默认编码(打印sys.getdefaultencoding())时,它会返回你使用的是ASCII。如果您更改为UTF-8,它将不起作用,这取决于变量的内容。 我找到了另一种方法:

import sys
reload(sys)  
sys.setdefaultencoding('Cp1252')

Encode将unicode对象转换为字符串对象。我认为你正在尝试编码一个字符串对象。首先将结果转换为unicode对象,然后将该unicode对象编码为'utf-8'。 例如

    result = yourFunction()
    result.decode().encode('utf-8')

Got a same error and this solved my error. Thanks! python 2 and python 3 differing in unicode handling is making pickled files quite incompatible to load. So Use python pickle's encoding argument. Link below helped me solve the similar problem when I was trying to open pickled data from my python 3.7, while my file was saved originally in python 2.x version. https://blog.modest-destiny.com/posts/python-2-and-3-compatible-pickle-save-and-load/ I copy the load_pickle function in my script and called the load_pickle(pickle_file) while loading my input_data like this:

input_data = load_pickle("my_dataset.pkl")

load_pickle函数在这里:

def load_pickle(pickle_file):
    try:
        with open(pickle_file, 'rb') as f:
            pickle_data = pickle.load(f)
    except UnicodeDecodeError as e:
        with open(pickle_file, 'rb') as f:
            pickle_data = pickle.load(f, encoding='latin1')
    except Exception as e:
        print('Unable to load data ', pickle_file, ':', e)
        raise
    return pickle_data