Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79884

Re: % symbol in python

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From alex23 <wuwei23@gmail.com>
Newsgroups comp.lang.python
Subject Re: % symbol in python
Date Wed, 29 Oct 2014 15:25:45 +1000
Organization A noiseless patient Spider
Lines 46
Message-ID <m2ptoj$hnv$1@dont-email.me> (permalink)
References <b80ee29c-e23a-4f11-9c65-746f7d74c347@googlegroups.com> <mailman.15276.1414547634.18130.python-list@python.org> <9a7bada3-6a2c-49c1-9806-cbf690a350b8@googlegroups.com> <mailman.15280.1414555330.18130.python-list@python.org> <77e77ae3-315e-4c3e-aedf-f501c8a217a7@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Injection-Date Wed, 29 Oct 2014 05:25:39 +0000 (UTC)
Injection-Info mx02.eternal-september.org; posting-host="f141bde4d1e4edae3516bb9f1d8e176c"; logging-data="18175"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/kleZQoF3osGaJzktChXgD"
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
In-Reply-To <77e77ae3-315e-4c3e-aedf-f501c8a217a7@googlegroups.com>
Cancel-Lock sha1:Zr4ejl9AdtcrdQKDJV/a45cpwxE=
Xref csiph.com comp.lang.python:79884

Show key headers only | View raw


On 29/10/2014 2:41 PM, satishmlmlml@gmail.com wrote:
 > kindly let me know what is $ROWS$ along with % symbol's meaning

It's a token, a static value added to the template to indicate where 
additional data will be added.

So $ROW$ in this section:

>   <table>
>   <tr><th>key<td><input type=text name=key value="%(key)s">
>   $ROWS$
 >   </table>

Will be replaced by whatever rowshtml contains at this point:

>   replyhtml = replyhtml.replace('$ROWS$', rowshtml)

Of note: the template section above has opening tags for <tr>, <th> and 
<td> but no closing ones. It's not valid html.

Also I don't think this is doing what you think it is:

 > rowhtml = '<tr><th>%s<td><input type=text name=%s value="%%(%s)s">\n'
 > rowshtml = ''
 > for fieldname in fieldnames:
 >  rowshtml += (rowhtml % ((fieldname, ) * 3))
 >  replyhtml = replyhtml.replace('$ROWS$', rowshtml)

After the first fieldname ('name' in this case), the token $ROWS$ will 
be replaced by the string :

     '<tr><th>name<td><input type=text name=name value="%%(name)s">\n'.

On the second iteration of the loop, when fieldname is 'age', rowshtml 
will equal:

      '<tr><th>name<td><input type=text name=name 
value="%%(name)s">\n<tr><th>age<td><input type=text name=age 
value="%%(age)s">\n'

...but nothing will happen with it as $ROWS$ has already been replaced 
during the first time through the loop. Either the `replyhtml = ...` 
line is mis-indented or the code is terribly broken.

Unless this is for homework, I highly recommend just using one of the 
many templating libraries that exist for Python, such as Jinja2.

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

% symbol in python satishmlmlml@gmail.com - 2014-10-28 14:41 -0700
  Re: % symbol in python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-28 22:17 +0000
  Re:% symbol in python Dave Angel <davea@davea.name> - 2014-10-28 21:57 -0400
    Re: % symbol in python satishmlmlml@gmail.com - 2014-10-28 20:51 -0700
      Re: % symbol in python Chris Angelico <rosuav@gmail.com> - 2014-10-29 15:02 +1100
        Re: % symbol in python satishmlmlml@gmail.com - 2014-10-28 21:41 -0700
          Re: % symbol in python alex23 <wuwei23@gmail.com> - 2014-10-29 15:25 +1000
            Re: % symbol in python satishmlmlml@gmail.com - 2014-10-28 22:48 -0700
              Re: % symbol in python Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-29 08:40 +0000

csiph-web