[Tutor] print method in Python2.7 problem
Alan Gauld
alan.gauld at btinternet.com
Sun Mar 15 19:29:06 CET 2015
To be fair to Doug his mails aren't cp,ing through to the list fully.
I susp3ect because he is using an Outlook message format with lots of
fancy formatting and a background all of which are being sent as
attachments...
Here is his text (with a lot of extraneous whitespace removed):
---------------------
For some reason my e-mails to this forum seem to have a problem,
I may sign up again to see if something is wrong with my account.
Mean while can the moderator cut and paste so members can see my post?
Thank you. Do you know what my account problem is? Thank you, Doug
Basberg dbasberg at comcast.net
I am attempting to write web pages in Python and inserting
numbers from a dictionary read in this program and populated
in another using a classes methods for read and write.
In this process, I found that in Python 2.7 on my Raspberry Pi,
I have a problem with a print statement. I have attached a
very small test program exhibiting the same problem. The
error message tells me that the semicolon is a problem.
It is in a single quoted string, so it should not be interpreted ??
I know that %xxx is interpreted inside strings of print
statements. Are there other special characters (like %) that
I am not aware of?
I can sure use some help with this. Thanks, Doug
File attached and shown as text below:
Stat = {'Vbatt': 51.24, 'Ichrg': 6.75}
print ' <td style="text-align: center;">SOLAR PANEL VOLTAGE</td>'
print(' DSB "HI;" %s') % (str(Stat['Vbatt']))
print(' <td style="width: 50%; text-align: center;">%s</td>') %
(str(Stat['Vbatt']))
Running the program:
$ python dsbtest02.py
<td style="text-align: center;">SOLAR PANEL VOLTAGE</td>
DSB "HI;" 51.24
Traceback (most recent call last):
File "dsbtest02.py", line 5, in <module>
print(' <td style="width: 50%; text-align: center;">%s</td>') %
(str(Stat['Vbatt']))
ValueError: unsupported format character ';' (0x3b) at index 24
---------------------
The solution (to the messaging issue) is if Doug uses plain text
for posts to the list in future.
The solution for the code issue is, as Steven says,
to use %% to escape the percent sign.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list