Python - 'ascii' codec can't decode byte

前端 未结 7 607
旧巷少年郎
旧巷少年郎 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: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

提交回复
热议问题