replace text in unicode string

S

Svennglenn

I'm having problems replacing text in a
unicode string.
Here's the code:

# -*- coding: cp1252 -*-

titel = unicode("ä", "iso-8859-1")
print titel
print type(titel)

titel.replace("ä", "a")


When i run this program I get this error:

titel.replace("ä", "a")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0:
ordinal not in range(128)


How can i replace text in the Unicode string?
 
D

Dan Bishop

Svennglenn said:
I'm having problems replacing text in a
unicode string.
Here's the code:

# -*- coding: cp1252 -*-

titel = unicode("ä", "iso-8859-1")
print titel
print type(titel)

titel.replace("ä", "a")

When i run this program I get this error:

titel.replace("ä", "a")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0:
ordinal not in range(128)

How can i replace text in the Unicode string?

titel = titel.replace(u"ä", "a")
 
J

John Machin

To the OP:
This is not causing the later problem, but it's evidence of the wrong
mindset for a start. You have just lied to the interpreter. You said
that your script was encoded using cp1252, but then you tried to pass
off a string constant as iso-8859-1!!! They are not exactly the same
repertoire. You need to make up your mind what character repertoire
your application should be confined to, and then apply that
restriction rigorously.

To get over the error message, all you need to do is this:

titel = u"ä"

.... and didn't I (and/or somebody else) tell you this only a few days
ago?

titel = titel.replace(u"ä", "a")

To Dan:
Fortuitously this works but if the OP wanted to change it to (say) an
umlauted-u then it would have thrown another UnicodeDecodeError.

Everybody please get into the habit of using u"blah blah" when you're
working with Unicode.

Like this:

titel = titel.replace(u"ä", u"a")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top