[Python-de] UTF-8 Python Datei
Gerald Klix
Gerald.Klix at klix.ch
Mon Apr 25 21:19:46 CEST 2005
Albert Hermeling schrieb:
> Guten Abend,
>
> ich habe heute ein kleines Skript geschrieben das aus einer Datei ein Wort
> ließt und dieses mit dem gleichen Wort in dem Skript vergleicht. Beide
> Dateien (Text und Skript!) sind in UTF-8 gespeichert worden. Die Textdatei
> wird mit codecs.open als Unicode Datei geöffnet.
>
> Hier das Skript:
>
> #!/usr/bin/python
> #-*- coding: UTF-8 -*-
>
> import codecs
>
> f = codecs.open("text.txt","rb","utf8").read()
>
> print type(f)
> print type("Äpfel")
>
> if f == "Äpfel":
> print "Das ist Richtig"
>
> Hier die Datei:
>
> Äpfel
>
>
> Wenn ich das Skript Starte bekomme ich folgende Meldung:
>
> <type 'unicode'> <------- Richtig!!
> <type 'str'> <------- Warum ist das ein String???
> Traceback (most recent call last):
> File "./anfuerungszeichen3.py", line 11, in ?
> if f == "Ãpfel":
Das ist der str^
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0:
> ordinal not in range(128)
>
> Es scheint also so das Python intern das Unicode Skript in ein ASCII Skript
> umwandelt wieso?
Tuts nicht ;)
Schreib
if f == u"Äpfel":
HTH,
Gerald
>
> Mit freundlichen Grüßen
>
> Albert
>
> _______________________________________________
> python-de maillist - python-de at python.net
> http://python.net/mailman/listinfo/python-de
--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634