Home » Odeon Blogs » Liviu, Agile Bear »

Unicode to Ascii Conversion

Unicode to Ascii Conversion

It's not just once that I stumbled upon this error when trying to work with strings that have special characters (é, etx.):

  1. UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)

Here's what I found to fix this:

  1. from django.utils.encoding import force_unicode
  2. html = force_unicode(str)
  3. html = html.encode('ascii', 'xmlcharrefreplace')

This way, the special characters are transformed in html friendly version (é etx.).


Categories: Django Python

Tagged as: django python unicode


  1. Excellent Post, nice trick
    I keep reading your posts.

Leave a Comment :




Page generated in: 0.13s