Python - 'ascii' codec can't decode byte

前端 未结 7 605
旧巷少年郎
旧巷少年郎 2021-01-30 17:08

I\'m using Python 2.6 and Jinja2 to create HTML reports. I provide the template with many results and the template loops through them and creates HTML tables

When callin

相关标签:
7条回答
  • 2021-01-30 17:21

    ASCII is a 7-bit code. The value 0xC4 cannot be stored in 7 bits. Therefore, you are using the wrong encoding for that data.

    0 讨论(0)
  • 2021-01-30 17:23

    Try to add this:

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

    It fixed my problem, good luck.

    0 讨论(0)
  • 2021-01-30 17:28

    If you get an error with a string like "ABC", maybe the non-ASCII character is somewhere else. In the template source perhaps?

    In any case, use Unicode strings throughout your application to avoid this kind of problems. 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. If your source is a file, use the StreamReader class in the codecs module.

    If you're unsure about the difference between Unicode strings and regular strings, read this: http://www.joelonsoftware.com/articles/Unicode.html

    0 讨论(0)
  • 2021-01-30 17:30

    Simple strings may contain UTF-8 character bytes but they are not of type unicode. This can be fixed by "decode" which converts str to unicode. Works in Python 2.5.5.

    my_string_variable.decode("utf8")

    0 讨论(0)
  • 2021-01-30 17:35

    Or you may do

    export LANG='en_US.UTF-8'
    

    in your console where you run the script.

    0 讨论(0)
  • 2021-01-30 17:39

    From 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.

    So wherever you set result.result_str, you need to make it unicode, e.g.

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

    (If your bytes were utf8 encoded unicode)

    or

    result.result_str = u"my string"
    
    0 讨论(0)
提交回复
热议问题