[Zope] parsing a textfile line by line

sean.upton@uniontrib.com sean.upton@uniontrib.com
Mon, 19 Feb 2001 11:34:17 -0800


Try an external method using regsub (regular exp. subsitutions, split, et=
c);
search for regsub.py in your Zope install to see a list of functions...  =
You
could pass the text of your object to a custom method to parse the text...

Sean

-----Original Message-----
From: Thomas =3D?UNKNOWN?Q?M=3DFChlens?=3D [mailto:tomeins@yahoo.com]
Sent: Saturday, February 17, 2001 4:58 AM
To: Joh Johannsen
Cc: zope@zope.org
Subject: Re: [Zope] parsing a textfile line by line


Dear Joh,

thank you  v e r y  much for your help.  Zope happily
swallowed the code you suggested without complaining.=20
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,
>=20
> I do this using an external python method:
>=20
>=20
> def LoadFile(filename):
>   try:
>     filein =3D open(filename, 'r')
>   except IOError:
>     return []
>   else:
>     result =3D filein.readlines()
>     filein.close()
>     return result
>=20
> Then from DTML, you get it line-by-line:
>=20
>  <dtml-in
> "LoadFile(filename=3D'/var/zope/var/test.file')">
>     <dtml-call
> "REQUEST.set('entry',_['sequence-item'])">
>=20
>    ... at this point, you have a dtml-var called
> "entry", one for each
> line of your file
>  </dtml-in>
>=20
> 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..)
>=20
> 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.
>=20
> Also, its kind of nice for running external programs
> from an external
> method:
>=20
> def runit(p1,p2):
>   result =3D os.popen(
> "/usr/local/farm9/bin/someprogram %s %s" % (p1,p2)
> ).readlines()
>   return result
>=20
> Then you can get the results line-by-line exactly
> the same way as you do
> from reading the file.
>=20
> Regards,
>=20
> JJ
>=20
> "Thomas M=FChlens" wrote:
>=20
> > 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=20
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 -=20
 http://lists.zope.org/mailman/listinfo/zope-announce
 http://lists.zope.org/mailman/listinfo/zope-dev )