473,378 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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(128)
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.

Nov 6 '05 #1
2 12399
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(128)

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>

Nov 6 '05 #2
> 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
Nov 6 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Peter Wilkinson | last post by:
Hello tlistmembers, I am using the encoding function to convert unicode to ascii. At one point this code was working just fine, however, now it has broken. I am reading a text file that has is...
4
by: Robin Siebler | last post by:
I have no idea what is causing this error, or how to fix it. The full error is: Traceback (most recent call last): File "D:\ScriptRuntime\PS\Automation\Handlers\SCMTestToolResourceToolsBAT.py",...
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special...
5
by: Mike Currie | last post by:
Can anyone explain why I'm getting an ascii encoding error when I'm trying to write out using a UTF-8 encoder? Thanks Python 2.4.3 (#69, Mar 29 2006, 17:35:34) on win32 Type "help",...
19
by: Thomas W | last post by:
I'm getting really annoyed with python in regards to unicode/ascii-encoding problems. The string below is the encoding of the norwegian word "fødselsdag". I stored the string as "fødselsdag"...
2
by: John Nagle | last post by:
I'm trying to clean up a bad ASCII string, one read from a web page that is supposedly in the ASCII character set but has some characters above 127. And I get this: File...
3
by: erikcw | last post by:
Hi all, I'm trying to parse an email message, but am running into this exception. Traceback (most recent call last): File "wa.py", line 336, in ? main() File "wa.py", line 332, in main...
3
by: jimscafe | last post by:
My software reads the directories and files on a server and then determines what changes have been made. I use python because I don't just blindly compare new and updated files but interpret the...
7
by: Gilles Ganault | last post by:
Hello Data that I download from the web seems to be using different code pages at times, and Python doesn't like this. Google returned a way to handle this, but I'm still getting an error:...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.