Thomas Kalka
Fri, 17 Nov 2006 00:09:55 -0800
print u'xc3xb6xc3xb6xc3xb6xc3xb6'.encode('latin1')
auf einem utf-8 - Terminal:
# das Ganze als Unicode-String
'xc3xb6xc3xb6xc3xb6xc3xb6' ist UTF-8 kodiertes 'öööö'
>>> t1 = u'xc3xb6xc3xb6xc3xb6xc3xb6'
>>> t1
u'xc3xb6xc3xb6xc3xb6xc3xb6'
>>> print t1
öööö
dh die einzelnen Zeichen werden ausgegeben, so weit in Ordnung
>>> t2 = t1.encode('latin1')
>>> t2
'xc3xb6xc3xb6xc3xb6xc3xb6'
siehe oben, das ist UTF-8 kodiertes 'öööö'
auf einem utf-8 - Terminal
>>> print t2
öööö
in Windows Console
>>> print t2
+Â+Â+Â+Â
d.h. je nach Terminal-Interpretation erscheint der String anders
Um dieses Problem zu lösen sind ja Unicode-Strings erfunden worden.
'öööö' ist in unicode immer u'xf6xf6xf6xf6'
siehe http://www.utf8-zeichentabelle.de/
Je nach Setting der Variable LANG wird das dann beim Ausgeben anders
gerendert:
(in einem UTF-8 Terminal, ubuntu-box):
[EMAIL PROTECTED]:~$ export LANG=POSIX
[EMAIL PROTECTED]:~$ python -c "import sys; print sys.stdout.encoding, u'xf6'"
ANSI_X3.4-1968 Traceback (most recent call last):
File "<string>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'xf6' in
position 0: ordinal not in range(128)
[EMAIL PROTECTED]:~$ export LANG=de_DE
[EMAIL PROTECTED]:~$ python -c "import sys; print sys.stdout.encoding, u'xf6'"
ISO-8859-1 ?
[EMAIL PROTECTED]:~$ export LANG=de_DE.utf8
[EMAIL PROTECTED]:~$ python -c "import sys; print sys.stdout.encoding, u'xf6'"
UTF-8 ö
Das zum Umgang von Python mit Unicode-Strings.
Das Problem bei Dir scheint aber nicht Python zu sein, sondern die
Verbindung von MySQL zu Python,
da da doppelt encodierte UTF-8 Strings ankommen.
Es sieht so aus, dass MySQL in UTF8 sendet,
Python davon ausgeht, dass das ISO8859 ist und das dann zu falschen
Unicode-Strings encoded.
Mit M2 meinte ich Deine Produktionsmaschine.