[Zope] parsing a textfile line by line

Joh Johannsen jojo@farm9.com
Fri, 16 Feb 2001 06:50:24 -0800


Hi Thomas,

I do this using an external python method:


def LoadFile(filename):
  try:
    filein = open(filename, 'r')
  except IOError:
    return []
  else:
    result = filein.readlines()
    filein.close()
    return result

Then from DTML, you get it line-by-line:

 <dtml-in "LoadFile(filename='/var/zope/var/test.file')">
    <dtml-call "REQUEST.set('entry',_['sequence-item'])">

   ... at this point, you have a dtml-var called "entry", one for each
line of your file
 </dtml-in>

NOTE: the above is a stripped-down, untested version of what I have, so
it may not work exactly as is, (though I think it does -- yes, I'm
naively optimistic..)

Also, if you are reading the line in from the external method, it might
be easier to parse the line there.  The list that is returned could be a
list-of-lists, then the above "entry" dtml-var would be a single list,
and you can access the elements of the list from dtml using entry[0],
etc.  don't know exact syntax for this.

Also, its kind of nice for running external programs from an external
method:

def runit(p1,p2):
  result = os.popen( "/usr/local/farm9/bin/someprogram %s %s" % (p1,p2)
).readlines()
  return result

Then you can get the results line-by-line exactly the same way as you do
from reading the file.

Regards,

JJ

"Thomas Mühlens" wrote:

> I stored a whole bunch of parameters in a textfile
> object (text/plain format) called params.  How can I
> read it into a string variable line by line, that is,
>
> read line1 - parse line1
> read line2 - parse line2
> ... etc
>
> Is it memory efficient to do this with some <!--
> <dtml-in> </dtml-in> --> syntax?
>
> cheers,
> tom
>
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail - only $35
> a year!  http://personal.mail.yahoo.com/
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )