Seite 1 von 1

Problem mit replace

Verfasst: Montag 4. Mai 2009, 13:58
von Boldi
ich möchte als Wohnort zB den ort münchen abspeichern, da aber ä,ü & ö im namen nicht vorkommen dürfe, möchte ich diese mit der *.replace methode entfernen.
wohnort=wohnort.replace('ä','ae')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
aber es funktioniert nicht :cry:
wo liegt der fehler?

Verfasst: Montag 4. Mai 2009, 14:10
von numerix
Coding-Zeile fehlt:

Code: Alles auswählen

# -*- coding: iso-8859-15 -*-

wohnort = "Bärendorf"
wohnort = wohnort.replace('ä','ae')
print wohnort

Verfasst: Montag 4. Mai 2009, 14:17
von Boldi
danke für die wirklich schnelle antowrt :)

aber es geht immer noch nicht...

Code: Alles auswählen

wohnort=wohnort.replace(' ','_')  
wohnort=wohnort.replace('ä','ae')
wohnort=wohnort.replace('ö','oe')
wohnort=wohnort.replace('ü','ue')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)
Ich benutze Python 2.5.1(umstieg aktuell unmöglich)

Verfasst: Montag 4. Mai 2009, 14:23
von Dill
"wohnort" ist wahrscheinlich ein unicode string.

probier mal:

Code: Alles auswählen

wohnort=wohnort.replace(u'ä','ae')

Verfasst: Montag 4. Mai 2009, 15:10
von birkenfeld
numerix hat geschrieben:Coding-Zeile fehlt:

Code: Alles auswählen

# -*- coding: iso-8859-15 -*-

wohnort = "Bärendorf"
wohnort = wohnort.replace('ä','ae')
print wohnort
Coding-Zeile bringt nur was für Unicode-Strings.

Verfasst: Montag 4. Mai 2009, 15:12
von BlackJack
@Boldi: Schau Dir mal die `unicode.translate()`-Methode an.

Verfasst: Montag 4. Mai 2009, 15:26
von numerix
birkenfeld hat geschrieben:
numerix hat geschrieben:Coding-Zeile fehlt:

Code: Alles auswählen

# -*- coding: iso-8859-15 -*-

wohnort = "Bärendorf"
wohnort = wohnort.replace('ä','ae')
print wohnort
Coding-Zeile bringt nur was für Unicode-Strings.
Ohne Coding-Zeile erhalte ich folgenden Traceback:

Code: Alles auswählen

  File "test.py", line 1
SyntaxError: Non-ASCII character '\xe4' in file test.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Mit Coding-Zeile läuft es sauber durch ...

Verfasst: Montag 4. Mai 2009, 15:29
von Dill
ja klar, aber das ist ja ein anderes problem also das des OP...

Verfasst: Montag 4. Mai 2009, 15:36
von birkenfeld
numerix hat geschrieben: Ohne Coding-Zeile erhalte ich folgenden Traceback:

Code: Alles auswählen

  File "test.py", line 1
SyntaxError: Non-ASCII character '\xe4' in file test.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Mit Coding-Zeile läuft es sauber durch ...
Das schon, aber einen Unicodestring bekommst du trotzdem nicht. Und wenn du dann Bytestrings aus verschieden kodierten Dateien mischt, bekommst du undekodierbaren Salat.

Verfasst: Montag 4. Mai 2009, 15:45
von numerix
Dill hat geschrieben:ja klar, aber das ist ja ein anderes problem also das des OP...
Sicher, das ist längst klar - mein letztes Posting bezog sich auf das, was birkenfeld gepostet hat.

@birkenfeld: Wie sähe denn eine "gute" Lösung aus für meinen konkreten Fall?

Verfasst: Montag 4. Mai 2009, 17:45
von Leonidas

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-

wohnort = u"Bärendorf"
wohnort = wohnort.translate({
    ord(u'ä') : u'ae'
})
print wohnort

Verfasst: Dienstag 5. Mai 2009, 14:23
von Boldi
ich danke euch allen, es funktioniert nun problemlos