Jinja2 UTF-8 garbled solution

Original link: https://hsiaofeng.com/archives/185.html

original code

 with open(f"file.j2",'w+') as fout: content = template.render() fout.write(content)

solution

Change w+ at open() to wb , and call encode('utf-8') after template.render() ).

 with open(f"file.j2",'wb') as fout: content = template.render().encode('utf-8') fout.write(content)

This article is reproduced from: https://hsiaofeng.com/archives/185.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment