如何解决:“ UnicodeDecodeError:'ascii'编解码器无法解码字节”

本文翻译自:How to fix: “UnicodeDecodeError: 'ascii' codec can't decode byte”

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!

How to fix it? 如何解决?

In some other python-based static blog apps, Chinese post can be published successfully. 在其他基于python的静态博客应用程序中,中文帖子可以成功发布。 Such as this app: http://github.com/vrypan/bucket3 . 像这个程序: http : //github.com/vrypan/bucket3 In my site http://bc3.brite.biz/ , Chinese post can be published successfully. 在我的网站http://bc3.brite.biz/中 ,中文帖子可以成功发布。


#1楼

参考:https://stackoom.com/question/1Qece/如何解决-UnicodeDecodeError-ascii-编解码器无法解码字节


#2楼

This is the classic "unicode issue". 这是经典的“ unicode问题”。 I believe that explaining this is beyond the scope of a StackOverflow answer to completely explain what is happening. 我相信解释这一点超出了StackOverflow答案的范围,无法完全解释正在发生的事情。

It is well explained here . 这里有很好的解释。

In very brief summary, you have passed something that is being interpreted as a string of bytes to something that needs to decode it into Unicode characters, but the default codec (ascii) is failing. 在简短的摘要中,您已将某些内容解释为字节字符串,并将其解码为Unicode字符,但是默认编解码器(ascii)失败了。

The presentation I pointed you to provides advice for avoiding this. 我为您指出的演示文稿提供了避免这种情况的建议。 Make your code a "unicode sandwich". 使您的代码为“ unicode三明治”。 In Python 2, the use of from __future__ import unicode_literals helps. 在Python 2中,使用from __future__ import unicode_literals帮助。

Update: how can the code be fixed: 更新:如何修复代码:

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. 无论如何,它们都不是用ascii编码的,但是python会假设它们是ASCII并尝试将它们转换为unicode。 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. 您可以尝试一些常见的编码-例如UTF-8。 You tell unicode() the encoding as a second parameter: 您将unicode()的编码作为第二个参数:

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

#3楼

Finally I got it: 终于我明白了:

as3:/usr/local/lib/python2.7/site-packages# cat sitecustomize.py
# encoding=utf8  
import sys  

reload(sys)  
sys.setdefaulten
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值