简体   繁体   English

Python - 'ascii'编解码器无法解码字节

[英]Python - 'ascii' codec can't decode byte

I'm using Python 2.6 and Jinja2 to create HTML reports. 我正在使用Python 2.6和Jinja2来创建HTML报告。 I provide the template with many results and the template loops through them and creates HTML tables 我为模板提供了许多结果,模板循环遍历它们并创建HTML表

When calling template.render, I've suddenly started getting this error. 在调用template.render时,我突然开始收到此错误。

<td>{{result.result_str}}</td>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)

The strange thing is, even if I set result.result_str to a simple ascii string like "abc" for every result, I am still seeing this error. 奇怪的是,即使我将result.result_str设置为一个简单的ascii字符串,如每个结果的“abc”,我仍然看到这个错误。 I'm new to Jinja2 and Python and would appreciate any ideas on how I can go about investigating the problem to get to the root cause. 我是Jinja2和Python的新手,我很欣赏任何有关如何调查问题以找到根本原因的想法。

Try to add this: 尝试添加:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

It fixed my problem, good luck. 它解决了我的问题,祝你好运。

From http://jinja.pocoo.org/docs/api/#unicode 来自http://jinja.pocoo.org/docs/api/#unicode

Jinja2 is using Unicode internally which means that you have to pass Unicode objects to the render function or bytestrings that only consist of ASCII characters. Jinja2在内部使用Unicode,这意味着您必须将Unicode对象传递给渲染函数或仅包含ASCII字符的字节串。

So wherever you set result.result_str, you need to make it unicode, eg 因此,无论您在何处设置result.result_str,都需要将其设置为unicode,例如

result.result_str = unicode(my_string_variable, "utf8")

(If your bytes were utf8 encoded unicode) (如果你的字节是utf8编码的unicode)

or 要么

result.result_str = u"my string"

If you get an error with a string like "ABC", maybe the non-ASCII character is somewhere else. 如果您收到类似“ABC”的字符串错误,则非ASCII字符可能位于其他位置。 In the template source perhaps? 在模板源中也许?

In any case, use Unicode strings throughout your application to avoid this kind of problems. 在任何情况下,请在整个应用程序中使用Unicode字符串以避免此类问题。 If your data source provides you with byte strings, you get unicode strings with byte_string.decode('utf-8') , if the string is encoded in UTF-8. 如果数据源为您提供字节字符串,则使用byte_string.decode('utf-8')获取unicode字符串byte_string.decode('utf-8')如果字符串以UTF-8编码)。 If your source is a file, use the StreamReader class in the codecs module. 如果您的源是文件,请使用编解码器模块中的StreamReader类。

If you're unsure about the difference between Unicode strings and regular strings, read this: http://www.joelonsoftware.com/articles/Unicode.html 如果您不确定Unicode字符串和常规字符串之间的区别,请阅读: http//www.joelonsoftware.com/articles/Unicode.html

Just encountered the same problem in a piece of code which saves output from Jinja2 to HTML files: 刚刚在一段代码中遇到了同样的问题,它将Jinja2的输出保存为HTML文件:

with open(path, 'wb') as fh:
    fh.write(template.render(...))

It's easy to blame Jinja2, although the actual problem is in Python's open() which as of version 2.7 doesn't support UTF-8. 很容易归咎于Jinja2,虽然实际问题出在Python的open()中,从版本2.7开始不支持UTF-8。 The fix is as simple as: 修复很简单:

import codecs
with codecs.open(path, 'wb', 'utf-8') as fh:
    fh.write(template.render(...))

Simple strings may contain UTF-8 character bytes but they are not of type unicode. 简单字符串可能包含UTF-8字符字节,但它们不是unicode类型。 This can be fixed by "decode" which converts str to unicode. 这可以通过“解码”来解决,它将str转换为unicode。 Works in Python 2.5.5. 适用于Python 2.5.5。

my_string_variable.decode("utf8") my_string_variable.decode( “UTF8”)

ASCII is a 7-bit code. ASCII是一个7位代码。 The value 0xC4 cannot be stored in 7 bits. 值0xC4不能以7位存储。 Therefore, you are using the wrong encoding for that data. 因此,您对该数据使用了错误的编码。

Or you may do 或者你可能会这样做

export LANG='en_US.UTF-8'

in your console where you run the script. 在运行脚本的控制台中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:“ ascii”编解码器无法解码字节 - Python: 'ascii' codec can't decode byte Python - “ascii”编解码器无法解码字节 - Python - 'ascii' codec can't decode byte Python 2.7 UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码字节 - Python 2.7 UnicodeDecodeError: 'ascii' codec can't decode byte Python(nltk)-UnicodeDecodeError:“ ascii”编解码器无法解码字节 - Python (nltk) - UnicodeDecodeError: 'ascii' codec can't decode byte Python - 'ascii'编解码器无法解码byte \ xbd的位置 - Python - 'ascii' codec can't decode byte \xbd in position UnicodeDecodeError:“ ascii”编解码器无法在Python中解码字节 - UnicodeDecodeError: 'ascii' codec can't decode byte in Python UnicodeDecodeError:“ ascii”编解码器无法解码字节-Python - UnicodeDecodeError: 'ascii' codec can't decode byte - Python Python和Pandas:UnicodeDecodeError:“ ascii”编解码器无法解码字节 - Python and Pandas: UnicodeDecodeError: 'ascii' codec can't decode byte UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码字节... Python 2.7和 - UnicodeDecodeError: 'ascii' codec can't decode byte … Python 2.7 and Python连接字符串-UnicodeDecodeError:&#39;ascii&#39;编解码器无法解码字节 - Python concatenating strings - UnicodeDecodeError: 'ascii' codec can't decode byte
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM