RE: [Zope] parsing a textfile line by line
Try an external method using regsub (regular exp. subsitutions, split, etc); 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 =?UNKNOWN?Q?M=FChlens?= [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. 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/ _______________________________________________ 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 )
participants (1)
-
sean.upton@uniontrib.com