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

J

Jaap van Wingerde

# -*- 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?
 
S

Stefan Behnel

Jaap said:
# -*- 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?

By using unicode.replace() instead of str.replace(), i.e.

Omschrijving = Omschrijving.replace("priv?", 'privé')

Stefan
 
J

Jaap van Wingerde

Stefan said:
Omschrijving = Omschrijving.replace("priv?", 'privé')

I Thank you, this works now, but I get a new error message.

.....
import codecs
file = "postbank.csv"
output = "%s.eb" % file
outfile = codecs.open(output, "w", "utf_8")
Omschrijving = u'priv? assuranti?n' # string from postbank.csv
Omschrijving = Omschrijving.replace("priv?", 'privé')
Omschrijving = Omschrijving.replace("Assuranti?n", 'Assurantiën')
outfile.write (Omschrijving)

"Traceback (most recent call last):
File "/home/jaap/Desktop/unicode.py", line 9, in <module>
outfile.write (Omschrijving)
File "/usr/lib/python2.5/codecs.py", line 638, in write
return self.writer.write(data)
File "/usr/lib/python2.5/codecs.py", line 303, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4:
ordinal not in range(128)"
 
S

Stefan Behnel

actually, make that

Omschrijving = Omschrijving.replace(u"priv?", u'privé')

(mind the u"...")

....
import codecs
file = "postbank.csv"
output = "%s.eb" % file
outfile = codecs.open(output, "w", "utf_8")
Omschrijving = u'priv? assuranti?n' # string from postbank.csv
Omschrijving = Omschrijving.replace("priv?", 'privé')
Omschrijving = Omschrijving.replace("Assuranti?n", 'Assurantiën')

I guess you mixed up the case here.

outfile.write (Omschrijving)

"Traceback (most recent call last):
File "/home/jaap/Desktop/unicode.py", line 9, in <module>
outfile.write (Omschrijving)
File "/usr/lib/python2.5/codecs.py", line 638, in write
return self.writer.write(data)
File "/usr/lib/python2.5/codecs.py", line 303, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4:
ordinal not in range(128)"

Does this help?

outfile = codecs.open(output, "wb", encoding="UTF-8")

(mind the "wb" for 'write binary/bytes')

Looks like you'd be happier with Python 3.0, BTW...

Stefan
 
J

Jaap van Wingerde

Stefan said:
Omschrijving = Omschrijving.replace(u"priv?", u'privé')
(mind the u"...")
outfile = codecs.open(output, "wb", encoding="UTF-8")
(mind the "wb" for 'write binary/bytes')
It works now!
Looks like you'd be happier with Python 3.0, BTW...
Python 3 is not in Debian Lenny.

With your help I made my first Python-script. This script saves me from
hours dumb work.

Thanks a lot!!
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top