f



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

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




-- 
Jaap van Wingerde
e-mail: 1234567890@vanwingerde.net
web: http://jaap.vanwingerde.net/
0
2/21/2009 1:35:05 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

4 Replies
5080 Views

Similar Articles

[PageSpeed] 42

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?

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

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

Stefan
0
stefan_ml (868)
2/21/2009 1:55:54 PM
Stefan Behnel wrote:
> 	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)"





0
2/21/2009 2:43:37 PM
Jaap van Wingerde wrote:
> Stefan Behnel wrote:
>>     Omschrijving = Omschrijving.replace("priv?", 'privé')

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
0
stefan_ml (868)
2/21/2009 3:00:41 PM
Stefan Behnel wrote:
> 	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!!




-- 
Jaap van Wingerde
e-mail: 1234567890@vanwingerde.net


0
2/21/2009 4:16:58 PM
Reply:

Similar Artilces:

'is not' or '!='
A newbie question to you; what is the difference between statements like: if x is not None: and if x != None: Without any context, which one should be preferred? IMHO, the latter is more readable. On 2014-08-18 21:35, ElChino wrote: > A newbie question to you; what is the difference between statements > like: > if x is not None: > and > if x != None: > > Without any context, which one should be preferred? > IMHO, the latter is more readable. > "x == y" tells you whether x and y refer to objects that are equal. "x is y&qu...

'^=' and '~='?
Hello, What is the difference between '^=' and '~='? Thanks, Duckhye ...

replacing '/' with '\\'
I have a MKS ksh shell script running on a Windows box. In some scripts I have to convert a UNIX filename into a DOS filename, that is replace the '/' with '\\'. For example, replace /opt/siips/archive/text.dat with \\opt\\siips\\archive\\text.dat. I've tried using sed like $FILE='echo $FILE|sed "s/\//\\\/g"' which got me \opt\siips\archive\text.dat. It's close but I still need the second \. Does anyone have any suggestion? Or tell me where I've gone wrong? Thanks -- Steffen On 9 Dec 2004 20:14:21 -0800, sajohn52@yahoo.com <sajohn52@yahoo....

replacement for '{' and '}' ?
I am still playing around with what kind of syntax I would like to mark up my documents. Are there equivalent long substitutes for { and } when they are not used to describe arguments to functions? Something like \begin{group} and \end{group}. In other words, if I could force myself to write, say, \begin{group} \it ... \end{group} instead of {\it ... }, then I believe I could identify from the markup context what is an argument that belongs to a just invoked macro and what is text. {Of course, in this context, \textit{...} would be better.} No more ambiguity whether a in \myfunction{a} i...

Replacing ',' with '.'
Hello, I have a huge amount of numbers in a .txt file. The numbers are in the form 2,43252e+1. I need to replace the , with . How should I do this? I'd prefer some import method that does this during the import procedure. -Janne Hi, I guess you import the data as text and convert it then to numbers. Try 'strrep' before you convert the text to numbers. Tobias Jake the Snake schrieb: > Hello, > > I have a huge amount of numbers in a .txt file. The numbers are in the form 2,43252e+1. I need to replace the , with . How should I do this? I'd prefer some import method...

'''''''''''''The Running Update/Append Queries Using VBA code Ordeal''''''''''''''
Hello fellow programmers, I am trying to run an append/update query from code, a command button on a form initiates the queries. the format i am using is; _____________________________________________________ SELECT "criteria" FROM "criteria" WHERE "criteria" UPDATE/APPEND "field selections" RecordSource "qryExample" = above text strings" _______________________________________________________________________ When i am running a SELECT query in this manner it works fine with no problems, and accepts the values of specified linked for...

'''''''''''''The Running Update/Append Queries Using VBA code Ordeal'''''''''''''' #2
Hi, Thanks for ur help there HJ. I know how to do the tasks you specified there. I would like for the update query to use field values from some of the fields on the form (frmInvoices) such as InvoiceNumber, DateFrom, DateTo. My problem is that an append/update query can't find the values in the open Form (frmInvoices) when I specify them as; [Forms]![frmInvoices]![InvoiceNumber] a select query has no problem finding the field values on a form. please help. Aaron Hi Aaron, Could you post the entire code that you are having trouble with? Now it is not possible to see what goes wron...

if str_mo not in ('','.') and str_da not in ('','.') and str_yy not in ('','.') Any shorter ?
Hi, there. =20 I'm just curious if it ever dawned on anybody how to abbreviate this line : if str_mo not in ('','.') and str_da not in ('','.') and str_yy not in ('','.')=20 =20 Igor Kurbeko Clinical Programmer Analyst 678 336 4328 ikurbeko@atherogenics.com =20 no brain no pain =20 how about: if not (str_mo in ('','.') or str_da in ('','.') or str_yy in ('','.')) OR if not (missing(str_mo) or missing(str_da) or missing(str_yy)) Eric On 22 Oct 03 21:13:37 GMT, ikurbeko@ATHER...

A function with 'and' , 'not' , 'null' , 'car' and 'cdr'
What's this ? (defun enigma (x) (and (not (null x)) (or (null (car x)) (enigma (cdr x))))) "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983 On Wed, Oct 07 2015, CAI GENGYANG wrote: > What's this ? > > > (defun enigma (x) > (and (not (null x)) > (or (null (car x)) > (enigma (cdr x))))) Bad taste? It returns T if the list X contains nil as an element. It would be clearer to write (some #'null x). Helmut CAI GENGYANG ...

Re: if str_mo not in ('','.') and str_da not in ('','.') and str_yy not in ('','.') Any shorter ?
OR you could use ARRAY data new; set old; array igor $ (*) str_mo str_da str_yr; do over igor; if igor ~in (' ','.') then do; end; run; Prasad Ravi Igor Kurbeko <ikurbeko@ATHEROGENIC To: SAS-L@LISTSERV.UGA.EDU S.COM> cc: Sent by: "SAS(r) Subject: if str_mo not in ('','.') and str_da not in ('','.') and str_yy ...

error: expected '=', ',', ';', 'asm' or '__attrib
Hi I'm trying to compile an ADC Driver & come acrosss the following error. I've no experience writing drivers before, and hence have no clue how to fix it. Hope someone out there has encountered the problem & suggesst a fix for the same. The Error is I get is : qadc.c: At top level: qadc.c:97: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'qadc_read' make: *** [qadc.o] Error 1 [root@localhost qadc]# ########################################################################### ADC Driver Code ####################...

error: expected '=', ',', ';', 'asm' or '__attrib
Hi I'm trying to compile an ADC Driver & come acrosss the following error. I've no experience writing drivers before, and hence have no clue how to fix it. Hope someone out there has encountered the problem & suggesst a fix for the same. The Error is I get is : qadc.c: At top level: qadc.c:97: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'qadc_read' make: *** [qadc.o] Error 1 [root@localhost qadc]# ########################################################################### ADC Driver Code ##...

Re: '^=' and '~='?
Duckhye, According to the doc ( http://xrl.us/befwjx ) they, and one other set of characters, and the mnemonic 'NE' all represent 'NOT EQUAL'. Art ------- On Wed, 11 Feb 2009 16:52:40 -0600, Duck-Hye Yang <dyang@CHAPINHALL.ORG> wrote: >Hello, >What is the difference between '^=' and '~='? > >Thanks, >Duckhye ...

Does '!=' equivelent to 'is not'
I'm a bit confusing about whether "is not" equivelent to "!=" if a != b: ... if a is not b: ... What's the difference between "is not" and "!=" or they are the same thing? pirata wrote: > I'm a bit confusing about whether "is not" equivelent to "!=" > > if a != b: > ... > > if a is not b: > ... > > > What's the difference between "is not" and "!=" or they are the same thing? No, they are not the same thing. == and != test to see if the *value* of two ...

Web resources about - TypeError: descriptor 'replace' requires a 'str' object but received a 'unicode' - comp.lang.python

Resources last updated: 2/4/2016 2:20:01 PM