Printing non-ascii characters in python/jinja

南楼画角 提交于 2019-12-06 01:58:44

From Jinja docs:

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

mylist = [u'some text \xc3']
schlamar

You should never open an encoded file and not decode it.

You should either read the encoding from curl (e.g. with -i or -H option) and parse the HTTP headers or the output file if the encoding is not specified in the headers.

Or as an alternative to curl you can use the requests library which don't require writing to a file. Fetching a web resource will look like:

>>> r = requests.get('http://python.org')
>>> r.content
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML...

Where content is already encoded following the HTTP specification.

As an last approach you could guess an encoding and replace unknown chars. This would be the most easy solution to implement. For example:

with codecs.open(filename, encoding='utf-8', errors='replace') as fobj:
    ...

Your approach will always loose information (if there are non ascii chars). My first two approaches never and the last one only if the guessed encoding is wrong.

I figured it out. The key is to do str.encode('string-escape')

So, I did this:

template = Template('{{ list[0].encode("string-escape") }}')

And that worked.

jla is right for my case.

I use utf-8 for the python source files, so use u prefix sovled my problem.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!