reading text based config file
Hi there, what's the best way to read a flat file like this line by line and split each line to three variables? 1:menuname:url 1.1:submenuname:url 1.2:submenuname:url 2:nenuname:url 2.1:sub ... background: i'd like to create a global navigation dtml-medthod. Only the configuration file should be replaced depending on the position in the file system. Thank you Marcus
Hi Marcus, this would be the wrong way[tm] to do the job. Unlike a classic, file based webserver zope knows all its objects. So the best way is to let zope calculate the navigation. No flat file reading, no problem with parsing them and no problem with writing them :-) You can use the tree-tag or dtml-in to do this. look for "objectValues" and stuff in the documentation. HTH Tino Wildenhain
Hi there,
what's the best way to read a flat file like this line by line and split each line to three variables?
1:menuname:url 1.1:submenuname:url 1.2:submenuname:url 2:nenuname:url 2.1:sub ...
background: i'd like to create a global navigation dtml-medthod. Only the configuration file should be replaced depending on the position in the file system.
Thank you Marcus
_______________________________________________ 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 )
Do a split on the ':'. It will give you a list containing the three parts. [Marcus Schopen] what's the best way to read a flat file like this line by line and split each line to three variables? 1:menuname:url 1.1:submenuname:url 1.2:submenuname:url 2:nenuname:url 2.1:sub ...
Hi, yes. But I've problems with the line feed character. My first steps: <dtml-with "_.namespace(var1=_.string.split(_['configfile'],':'))"> <dtml-var var1> </dtml-with> I'd like to write the content to an array. What's the best way to do that. Can't find it out. Thank you - Marcus - Zitiere "Thomas B. Passin" <tpassin@mitretek.org>:
Do a split on the ':'. It will give you a list containing the three parts.
[Marcus Schopen]
what's the best way to read a flat file like this line by line and split each line to three variables?
1:menuname:url 1.1:submenuname:url 1.2:submenuname:url 2:nenuname:url 2.1:sub ...
_______________________________________________ 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 )
"Thomas B. Passin" wrote:
Do a split on the ':'. It will give you a list containing the three parts.
[Marcus Schopen]
what's the best way to read a flat file like this line by line and split each line to three variables?
1:menuname:url 1.1:submenuname:url 1.2:submenuname:url 2:nenuname:url 2.1:sub ...
import string def readconfig(path): f = open(path) vars = [] for line in f.readlines(): vars.append(string.split(line,':')) f.close() return vars You could also use regular expressions, which might be faster, just harder to read. -- | Casey Duncan | Kaivo, Inc. | cduncan@kaivo.com `------------------>
Sorry, I assumed you had already read in your file and had access to each line. Just to be explicit, Casey's code would be placed in an External Method. More interesting, though, is how you expect to get the file. Will the filename be contained in a form that gets submitted to you, will you compute the filename, will it be hardcoded, will it be a "file" contained in the Zope database, or a regular file in the file system? The answers to questions like these will affect how you proceed. Cheers, Tom P [Casey Duncan]
"Thomas B. Passin" wrote:
Do a split on the ':'. It will give you a list containing the three
parts.
[Marcus Schopen]
what's the best way to read a flat file like this line by line and split each line to three variables?
1:menuname:url 1.1:submenuname:url 1.2:submenuname:url 2:nenuname:url 2.1:sub ...
import string
def readconfig(path): f = open(path) vars = [] for line in f.readlines(): vars.append(string.split(line,':')) f.close() return vars
what's the best way to read a flat file like this line by line and split each line to three variables?
1:menuname:url 1.1:submenuname:url 1.2:submenuname:url 2:nenuname:url 2.1:sub ...
You might also want to look at the TinyTablePlus product, http://www.zope.org/Members/hathawsh/TinyTablePlus for storing simple tabular information like this. Ivan
participants (5)
-
Casey Duncan -
Ivan Cornell -
Marcus Schopen -
Thomas B. Passin -
Tino Wildenhain