470,591 Members | 2,165 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,591 developers. It's quick & easy.

unicode error

I have this python code:
print >> htmlFile, "<div id=\"track" + unicode(1) + "\"
style=\"width: 200px; height:18px;\">";
But that caues this error, and I can't figure it out why. Any help is
appreicate
File "./run.py", line 193, in ?
print >> htmlFile, "<div id=\"track" + unicode(1) + "\"
style=\"width: 200px; height:18px;\">";
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 9:
ordinal not in range(128)

Thanks.

Mar 17 '06 #1
2 1890
Al************@gmail.com wrote:
I have this python code:
print >> htmlFile, "<div id=\"track" + unicode(1) + "\"
style=\"width: 200px; height:18px;\">";
But that caues this error, and I can't figure it out why. Any help is
appreicate
File "./run.py", line 193, in ?
print >> htmlFile, "<div id=\"track" + unicode(1) + "\"
style=\"width: 200px; height:18px;\">";
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 9:
ordinal not in range(128)

Thanks.

Hi,
I tried and it worked (wrote into the file:<div id="track1"style="width:
200px; height:18px;">).
Can you try to isolate exactly what part of the code is wrong ?
jm
Here is the complete code:
htmlfile=file('jmbc.txt','w')
print >> htmlfile, "<div id=\"track" + unicode(1) + "\"style=\"width:
200px; height:18px;\">";
htmlfile.close()
Mar 17 '06 #2
Al************@gmail.com wrote:
I have this python code:
print >> htmlFile, "<div id=\"track" + unicode(1) + "\"
style=\"width: 200px; height:18px;\">";
But that caues this error, and I can't figure it out why. Any help is
appreicate
File "./run.py", line 193, in ?
print >> htmlFile, "<div id=\"track" + unicode(1) + "\"
style=\"width: 200px; height:18px;\">";
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 9:
ordinal not in range(128)

Thanks.

You can make the code easier to read by using single quotes to quote
strings with double quotes inside:

print >> htmlFile, ('<div id="track' + unicode(1) +
'" style="width: 200px; height:18px;">')

Or even better:

print >> htmlFile, (u'<div id="track%s" '
u'style="width: 200px; height:18px;">') % unicode(1)

The unicode(1) confuses me -- you are converting an integer to its
string representation in unicode (do you know that?), not picking a
particular character.

print >> htmlFile, (u'<div id="track%d" style="width: 200px; '
u'height:18px;">') % (1,)

And if you don't mean to be writing unicode, you could use:

print >> htmlFile, ('<div id="track%d" style="width: 200px; '
'height:18px;">') % (1,)

--Scott David Daniels
sc***********@acm.org
Mar 17 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by ..... | last post: by
8 posts views Thread by Ivan Voras | last post: by
24 posts views Thread by ChaosKCW | last post: by
4 posts views Thread by Petr Jakes | last post: by
9 posts views Thread by Jim | last post: by
8 posts views Thread by rkellerjr | last post: by
7 posts views Thread by 7stud | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.