parsing a textfile line by line
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/
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 )
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/
"Thomas Mühlens" wrote:
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.
Good question. I would like to know that also. At the moment, I'm having a hard time understanding how to deal with zope objects from python code. I'd use zope objects alot more if I did understand this. Reading a textfile that is a zope object sounds like a good start. Anyone know how to do that? For me, that's the hardest thing about dealing with zope. For a while, it seems like you are doing the impossible very easily, then something very easy comes along, and it seems impossible. Regards, JJ
[Joh Johannsen] | Reading a textfile that is a zope object sounds like a good start. | Anyone know how to do that? I'm not quite sure if I understand. Would it satisfy you to have a Zope object with a text property, and read that? And what do you mean by «from Python»? As in Python Products, Python Scripts or Python Extensions?
The idea is simple: How can I access a textfile (which is a zope object n o t a file stored in some "var" folder on your harddisk) and interpret the data line by line (preferably not using external methods but using python scripts which are zope objects, too). I don't see any potential security violations (zope's paranoid sometimes) so it should be possible. This seems to be a difficult task in zope because, like Joh said, very simple processes have to be externalized (e.g. external methods) Well, once I start using external processes my code get's fragmented and scattered over scripts in various external folders. My vision, and that's why I'm using zope and not php, is to keep my code in one place (the zope object database file data.fs) and object oriented (it's a bummer to always change code in various places). Then again, it might just be my poor zope zen :) or thin documentation of zope to solve such simple problems. --- Erik Enge <erik+list@esol.no> wrote:
[Joh Johannsen]
| Reading a textfile that is a zope object sounds like a good start. | Anyone know how to do that?
I'm not quite sure if I understand. Would it satisfy you to have a Zope object with a text property, and read that? And what do you mean by �from Python�? As in Python Products, Python Scripts or Python Extensions?
__________________________________________________ Do You Yahoo!? Get personalized email addresses from Yahoo! Mail - only $35 a year! http://personal.mail.yahoo.com/
participants (3)
-
Erik Enge -
Joh Johannsen -
Thomas M�hlens