Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 99: ordinal not in range(128)

Reply
Thread Tools

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 99: ordinal not in range(128)

 
 
Francach
Guest
Posts: n/a
 
      11-06-2005
Hi,

I don't know what I'm doing wrong here.
I''m using Python 2.4 and py2exe. I get he following error:

Traceback (most recent call last):
File "notegui.pyc", line 34, in OnClose
File "brain.pyc", line 61, in setNote
File "points.pyc", line 151, in setNote
File "point.pyc", line 100, in writeNote
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in
position 99: ordinal not in range(12


The piece of code involved is:

noteFileObj = open(noteFile, "wb")
noteFileObj.write(note)
noteFileObj.close()


I would've thought that the 'b' option meant I can write any binary
code I like to the file,
but that's not so?

Thanks for any tips,
Martin.

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-06-2005
Francach wrote:

> I don't know what I'm doing wrong here.
> I''m using Python 2.4 and py2exe. I get he following error:
>
> Traceback (most recent call last):
> File "notegui.pyc", line 34, in OnClose
> File "brain.pyc", line 61, in setNote
> File "points.pyc", line 151, in setNote
> File "point.pyc", line 100, in writeNote
> UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in
> position 99: ordinal not in range(12
>
> The piece of code involved is:
>
> noteFileObj = open(noteFile, "wb")
> noteFileObj.write(note)
> noteFileObj.close()
>
> I would've thought that the 'b' option meant I can write any binary
> code I like to the file, but that's not so?


since you're getting a UnicodeEncodeError, the "note" object is probably
a Unicode string, not a "binary code".

to write Unicode strings to a file, you need to decide what encoding to
use, and encode the string on the way out. e.g.

nodeFileObj.write(note.encode("utf-8"))

to write it as a UTF-8 string.

</F>



 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      11-06-2005
> I would've thought that the 'b' option meant I can write any binary
> code I like to the file,
> but that's not so?


You can. But if you write a unicode-object (wich is an abstract data
type with no byte representation), it has to be converted to a string -
which you have to do either explicit. Or if you don't do it - it ill be
done automatically, using the system default encoding. Which is ascii,
most of the time.

So do

noteFileObj.write(note.encode("utf-8"))

Regards,

Diez
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
'ascii' codec can't encode character u'\xe4' in position 4: ordinalnot in range(128) balavignesh Python 0 11-08-2009 10:37 AM
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 10: ordinal not in range(128) Robin Siebler Python 4 10-08-2004 08:03 PM
Re: 'ascii' codec can't encode character u'\xf3' Martin Slouf Python 6 08-18-2004 06:23 AM
RE: 'ascii' codec can't encode character u'\xf3' Ben Last Python 0 08-17-2004 01:23 PM
'ascii' codec can't encode character u'\xf3' oziko Python 1 08-16-2004 11:12 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57