unicode issue

Mark Tolonen metolone+gmane at gmail.com
Wed Sep 30 10:24:29 EDT 2009


"Dave Angel" <davea at dejaviewphoto.com> wrote in message 
news:4AC328D4.3060200 at dejaviewphoto.com...
> gentlestone wrote:
>> Why don't work this code on Python 2.6? Or how can I do this job?
>>
>> _MAP =
>>     # LATIN
>>     u'À': 'A', u'Á': 'A', u'Â': 'A', u'Ã': 'A', u'Ä': 'A', u'Å': 'A',
>> u'Æ': 'AE', u'Ç':'C',
>>     u'È': 'E', u'É': 'E', u'Ê': 'E', u'Ë': 'E', u'Ì': 'I', u'Í': 'I',
>> u'Î': 'I',
>>     u'Ï': 'I', u'Ð': 'D', u'Ñ': 'N', u'Ò': 'O', u'Ó': 'O', u'Ô': 'O',
>> u'Õ': 'O', u'Ö':'O',
>>     u'Ő': 'O', u'Ø': 'O', u'Ù': 'U', u'Ú': 'U', u'Û': 'U', u'Ü': 'U',
>> u'Ű': 'U',
>>     u'Ý': 'Y', u'Þ': 'TH', u'ß': 'ss', u'à':'a', u'á':'a', u'â': 'a',
>> u'ã': 'a', u'ä':'a',
>>     u'å': 'a', u'æ': 'ae', u'ç': 'c', u'è': 'e', u'é': 'e', u'ê': 'e',
>> u'ë': 'e',
>>     u'ì': 'i', u'í': 'i', u'î': 'i', u'ï': 'i', u'ð': 'd', u'ñ': 'n',
>> u'ò': 'o', u'ó':'o',
>>     u'ô': 'o', u'õ': 'o', u'ö': 'o', u'ő': 'o', u'ø': 'o', u'ù': 'u',
>> u'ú': 'u',
>>     u'û': 'u', u'ü': 'u', u'ű': 'u', u'ý': 'y', u'þ': 'th', u'ÿ': 'y',
>>     # LATIN_SYMBOLS
>>     u'©':'(c)',
>>     # GREEK
>>     u'α':'a', u'β':'b', u'γ':'g', u'δ':'d', u'ε':'e', u'ζ':'z',
>> u'η':'h', u'θ':'8',
>>     u'ι':'i', u'κ':'k', u'λ':'l', u'μ':'m', u'ν':'n', u'ξ':'3',
>> u'ο':'o', u'π':'p',
>>     u'ρ':'r', u'σ':'s', u'τ':'t', u'υ':'y', u'φ':'f', u'χ':'x',
>> u'ψ':'ps', u'ω':'w',
>>     u'ά':'a', u'έ':'e', u'ί':'i', u'ό':'o', u'ύ':'y', u'ή':'h',
>> u'ώ':'w', u'ς':'s',
>>     u'ϊ':'i', u'ΰ':'y', u'ϋ':'y', u'ΐ':'i',
>>     u'Α':'A', u'Β':'B', u'Γ':'G', u'Δ':'D', u'Ε':'E', u'Ζ':'Z',
>> u'Η':'H', u'Θ':'8',
>>     u'Ι':'I', u'Κ':'K', u'Λ':'L', u'Μ':'M', u'Ν':'N', u'Ξ':'3',
>> u'Ο':'O', u'Π':'P',
>>     u'Ρ':'R', u'Σ':'S', u'Τ':'T', u'Υ':'Y', u'Φ':'F', u'Χ':'X',
>> u'Ψ':'PS', u'Ω':'W',
>>     u'Ά':'A', u'Έ':'E', u'Ί':'I', u'Ό':'O', u'Ύ':'Y', u'Ή':'H',
>> u'Ώ':'W', u'Ϊ':'I', u'Ϋ':'Y',
>>     # TURKISH
>>     u'ş':'s', u'Ş':'S', u'ı':'i', u'İ':'I', u'ç':'c', u'Ç':'C',
>> u'ü':'u', u'Ü':'U',
>>     u'ö':'o', u'Ö':'O', u'ğ':'g', u'Ğ':'G',
>>     # RUSSIAN
>>     u'а':'a', u'б':'b', u'в':'v', u'г':'g', u'д':'d', u'е':'e',
>> u'ё':'yo', u'ж':'zh',
>>     u'з':'z', u'и':'i', u'й':'j', u'к':'k', u'л':'l', u'м':'m',
>> u'н':'n', u'о':'o',
>>     u'п':'p', u'р':'r', u'с':'s', u'т':'t', u'у':'u', u'ф':'f',
>> u'х':'h', u'ц':'c',
>>     u'ч':'ch', u'ш':'sh', u'щ':'sh', u'ъ':'', u'ы':'y', u'ь':'',
>> u'э':'e', u'ю':'yu', u'я':'ya',
>>     u'А':'A', u'Б':'B', u'В':'V', u'Г':'G', u'Д':'D', u'Е':'E',
>> u'Ё':'Yo', u'Ж':'Zh',
>>     u'З':'Z', u'И':'I', u'Й':'J', u'К':'K', u'Л':'L', u'М':'M',
>> u'Н':'N', u'О':'O',
>>     u'П':'P', u'Р':'R', u'С':'S', u'Т':'T', u'У':'U', u'Ф':'F',
>> u'Х':'H', u'Ц':'C',
>>     u'Ч':'Ch', u'Ш':'Sh', u'Щ':'Sh', u'Ъ':'', u'Ы':'Y', u'Ь':'',
>> u'Э':'E', u'Ю':'Yu', u'Я':'Ya',
>>     # UKRAINIAN
>>     u'Є':'Ye', u'І':'I', u'Ї':'Yi', u'Ґ':'G', u'є':'ye', u'і':'i',
>> u'ї':'yi', u'ґ':'g',
>>     # CZECH
>>     u'č':'c', u'ď':'d', u'ě':'e', u'ň':'n', u'ř':'r', u'š':'s',
>> u'ť':'t', u'ů':'u',
>>     u'ž':'z', u'Č':'C', u'Ď':'D', u'Ě':'E', u'Ň':'N', u'Ř':'R',
>> u'Š':'S', u'Ť':'T', u'Ů':'U', u'Ž':'Z',
>>     # POLISH
>>     u'ą':'a', u'ć':'c', u'ę':'e', u'ł':'l', u'ń':'n', u'ó':'o',
>> u'ś':'s', u'ź':'z',
>>     u'ż':'z', u'Ą':'A', u'Ć':'C', u'Ę':'e', u'Ł':'L', u'Ń':'N',
>> u'Ó':'o', u'Ś':'S',
>>     u'Ź':'Z', u'Ż':'Z',
>>     # LATVIAN
>>     u'ā':'a', u'č':'c', u'ē':'e', u'ģ':'g', u'ī':'i', u'ķ':'k',
>> u'ļ':'l', u'ņ':'n',
>>     u'š':'s', u'ū':'u', u'ž':'z', u'Ā':'A', u'Č':'C', u'Ē':'E',
>> u'Ģ':'G', u'Ī':'i',
>>     u'Ķ':'k', u'Ļ':'L', u'Ņ':'N', u'Š':'S', u'Ū':'u', u'Ž':'Z'
>> }
>>
>> def downcode(name):
>>     """
>>     >>> downcode(u"Žabovitá zmiešaná kaša")
>>     u'Zabovita zmiesana kasa'
>>     """
>>     for key, value in _MAP.iteritems():
>>         name =ame.replace(key, value)
>>     return name
>>
>>
> Works for me:
>
> rrr = downcode(u"Žabovitá zmiešaná kaša")
> print repr(rrr)
> print rrr
>
> prints out:
>
> u'Zabovita zmiesana kasa'
> Zabovita zmiesana kasa
>
> I did have to add an encoding declaration as line 2 of the file:
>
> #-*- coding: latin-1 -*-
>
> and I had to convince my editor (Komodo) to save the file in utf-8.

Why decare latin-1 and save in utf-8?  I'm not sure how you got that to work 
because those encodings aren't equivalent.  I get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "testit.py", line 1
SyntaxError: encoding problem: utf-8

In fact, some of the characters in the above code don't map to latin-1.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u0150' in 
position
 309: ordinal not in range(256)
>>> import unicodedata as ud
>>> ud.name(u'\u0150')

-Mark






More information about the Python-list mailing list