[Zope] parsing a textfile line by line
Thomas Mühlens
tomeins@yahoo.com
Sat, 17 Feb 2001 04:57:34 -0800 (PST)
Dear Joh,
thank you v e r y much for your help. Zope happily
swallowed the code you suggested without complaining.
We seem to get along a lot better now :)
Originally I wanted to use the textfile as a zope
Object because I can't access the filesystem on the
server (yep, it's http://freezope.nipltd.net). Of
course it was no problem to parse the data locally and
import the results to the zope instance on the server.
Just out of curiosity --> even though my problem was
solved I wonder how this task would be accomplished
when the textfile is a zope object.
Regards,
tom
--- Joh Johannsen <jojo@farm9.com> wrote:
> 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/