Eine Frage habe ich noch, auch wenn sie nicht wirklich zum Thread-Thema passt: Ich hab's jetzt mal zum Testen mit einem deutschen Encoding probiert. Weiß jemand wieso ich hier zwei Zeichen zurückbekomme, mal ganz davon ab, dass keins davon ein 'ä' ist?
Code: Alles auswählen
sebastian@hardy:~$ python get_char_name.py 'bäh'
b : LATIN SMALL LETTER B
à : LATIN CAPITAL LETTER A WITH TILDE
¤ : CURRENCY SIGN
h : LATIN SMALL LETTER H
Der Quelltext dazu ist:
Code: Alles auswählen
import sys
import unicodedata
for char in sys.argv[1]:
char = char.decode('iso-8859-1')
print '%s : %s' % (char, unicodedata.name(char))
Das muss ja an der Shell (Bash) liegen, denn im Interpreter funktioniert es. Da allerdings mit utf-8:
Code: Alles auswählen
In [47]: unicodedata.name('ä'.decode('utf-8'))
Out[47]: 'LATIN SMALL LETTER A WITH DIAERESIS'
Setze ich utf-8 in den Quelltext ein, bekomme ich wiederum ne Exception:
Code: Alles auswählen
sebastian@hardy:~$ python get_char_name.py 'bäh'
b : LATIN SMALL LETTER B
Traceback (most recent call last):
File "get_char_name.py", line 5, in <module>
char = char.decode('utf-8')
File "/usr/lib/python2.5/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc3 in position 0: unexpected end of data
Hat jemand ne Idee wie meine Umlaute auch außerhalb des Interpreters richtig behandelt werden?