pythonunicodeasciiutfunicode-normalization

How to convert an UTF string with scandinavian characters to ASCII?


I would like to convert this string

foo_utf = u'nästy chäräctörs with å and co.' # unicode

into this

foo_ascii = 'nästy chäräctörs with å and co.' # ASCII

.

Any idea how to do this in Python (2.6)? I found unicodedata module but I have no idea how to do the transformation.


Solution

  • This really is a Django question, and not a python one. if the string is in one of your .py files, make sure that you have the following line on top of your file: -*- coding: utf-8 -*-

    furthermore, your string needs to be of type "unicode" (u'foobar')

    And then make sure that your html page works in unicode:

    <meta http-equiv="content-type" content="text/html;charset=utf-8" />

    That should do the whole trick. No encoding/decoding etc. necessary, just make sure that everything is unicode, and you are on the safe side.