Python - 'ascii' codec can't decode byte

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

    Just encountered the same problem in a piece of code which saves output from Jinja2 to HTML files:

    with open(path, 'wb') as fh:
        fh.write(template.render(...))
    

    It's easy to blame Jinja2, although the actual problem is in Python's open() which as of version 2.7 doesn't support UTF-8. The fix is as simple as:

    import codecs
    with codecs.open(path, 'wb', 'utf-8') as fh:
        fh.write(template.render(...))
    
    0 讨论(0)
提交回复
热议问题