TypeError: descriptor 'replace' requires a 'str' object but received a 'unicode'

Steve Holden steve at holdenweb.com
Sat Feb 21 10:27:54 EST 2009


Jaap van Wingerde wrote:
> # -*- coding: utf_8 -*-
> Omschrijving = u'priv? assuranti?n' # string from a bank.csv
> Omschrijving = str.replace(Omschrijving, "priv?", 'privé')
> Omschrijving = str.replace(Omschrijving, "Assuranti?n", 'Assurantiën')
> print Omschrijving
> 
> When I run this script I get the following message.
> 
> "Traceback (most recent call last):
>   File "/home/jaap/Desktop/unicode.py", line 3, in <module>
>     Omschrijving = str.replace(Omschrijving, "priv?", 'priv�')
> TypeError: descriptor 'replace' requires a 'str' object but received a
> 'unicode'"
> 
> How can I solve this?
> 

First of all, use the methods of the unicode type, not the str type.
Secondly, call the methods on an instance, not on the type (the instance
is passed automatically). Thirdly, use Unicode arguments to replace.

Omschrijving = u'priv? assuranti?n' # string from a bank.csv
Omschrijving = Omschrijving.replace(u"priv?", u'privé')
Omschrijving = Omschrijving.replace(u"assuranti?n", u'assurantiën')
print Omschrijving

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list