more newbie list questions

Sion Arrowsmith siona at chiark.greenend.org.uk
Thu Jul 14 08:14:49 EDT 2005


Bernhard Holzmayer  <Holzmayer.Bernhard at deadspam.com> wrote:
>googleboy wrote:
>> I have a cell.txt file that looks like this:
>> 
>> ++
>> The title is %title%.  <br><br>
>> The author is %author1% %author2% <br><br>
>> The Publisher is %publisher1% %publisher2% <br><br>
>> The ISBN is %ISBN% <br><br>
>> ++
>
>This looks like a DOS-batch-file. Maybe you'd better just leave it to
>DOS to populate it, just by exec-uting it  in an environment which 
>has title, authort1, ... set. ??
>
>On the other hand, if you need not relate to the cell.txt file, 
>you could just use something like
> 
>sAuth = "The author is %s" % author1

Or

sAuth = "The author is %(author1)s" % locals()

so cell.txt could be replaced by something like

++
The title is %(title)s.  <br><br>
The author is %(author1)s %(author2)s <br><br>
The Publisher is %(publisher1)s %(publisher2)s <br><br>
The ISBN is %(ISBN)s <br><br>
++

and you could do everything at once. Obviously you'd be better
off sticking author1 etc. into a dict instead of making them
local variables. This might also solve your problems with
"getting fields into Python" (as someone else said, it's not
entirely clear what your difficulties are):

field_names = ["title", "author1", "author2", "publisher", "ISBN"]
fields_dict = dict(zip(field_names, fields))
sTemplate % field_dict

>> I know how to do something like sAuth = re.sub('%author1%', author1,
>> sTemplate)

re.sub() is massive overkill. If you can't get where you want
with % as above, use str.replace instead:

sTemplate.replace('%author1%', author1)

(Er, "you" above refers to OP, not Bernhard.)

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list