Why doesn't this replace function work?

So I'm using this line of Python to replace some weird characters in a string;

title = title.replace('\xc3', 'e').replace('\xa9', 's')

The weird string is:

"B\xc3\xa9same Mucho"

It has some Spanish-style accents, and I figured it would be simpler to try to get rid of them instead of trying to implement the accents.

But it doesn't replace the affected parts.

What's wrong with the line?

Thanks!

evamvid

Answers


Assuming you're using Python 2.7, you're just having a classic bad encoding day. Python 2 is a little notorious for its Unicode(De|En)codeError. If you really want to replace those characters, observe that:

>>> utitle = title.decode('utf-8')
u'B\xe9same Mucho'

so

>>> utitle.replace(u'\xe9', 'e')
u'Besame Mucho'

But you really want to be dealing with unicode the whole time, and the characters there are really fine, so just do the decode.

This is one area in which Python 3 is much better than Python 2.


Need Your Help

How do I update the status bar from multiple viewmodels?

wpf viewmodel status wvvm

I have MainWindow with a statusbar and multiple user controls in it. Each user control has a viewmodel. How do I bind/update the statusbar text from multiple viewmodels?

Error while calculating the grand total of a column in SSRS

ssrs-2008 ssrs-2008-r2 ssrs-2012 ssrs-tablix ssrs-grouping

I have an SSRS report which is grouped on one a column. Here i have four columns on the report. First column is the grouped column, second is description, and third and fourth columns are those whose