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
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.
Try to add this:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
It fixed my problem, good luck.
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
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")
Or you may do
export LANG='en_US.UTF-8'
in your console where you run the script.
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"