UnicodeDecodeError: 'ascii' codec can't decode byte

Posted on in Programming

cover image for article

Although I was getting this output in relation to one of my Jinja2 templates, it really isn't a Jinja2 problem.

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 69: ordinal not in range(128)

The bit of code here should be doing nothing more than printing a float:

'{:,.2f}'.format(item.profit_created())

For whatever reason, whatever is being returned from profit_created() seems to contain a "â" (that's an "a" with a hat). Stack Overflow indicates two solutions for this problem. I decided to go with the second solution, buried in the comments:

from __future__ import unicode_literals

If that doesn't work, the other solution seems to work just fine, too.

import sys  
reload(sys)  
sys.setdefaultencoding("utf-8")

My Bookshelf

Reading Now

Other Stuff