Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 370
Juho Vepsäläinen Avatar asked Nov 16 '25 13:11

Juho Vepsäläinen


2 Answers

I don't think you can. Those "nästy chäräctörs" can't be encoded as ASCII, so you'll have to pick a different encoding (UTF-8 or Latin-1 or Windows-1252 or something).

like image 96
Will McCutchen Avatar answered Nov 18 '25 08:11

Will McCutchen


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.

like image 29
mawimawi Avatar answered Nov 18 '25 06:11

mawimawi