unicode error

A

Allerdyce.John

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.
 
J

jean-michel bain-cornu

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()
 
S

Scott David Daniels

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
(e-mail address removed)
 

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
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top