create/access dynamic growth list

sam sam.wun at authtec.com
Fri Mar 18 22:55:03 EST 2005


Larry Bates wrote:

> sam wrote:
> 
>>Hi,
>>
>>I have a configuration file need to be processed (read/write) by python.
>>Currently I the following method can only read and store data that
>>python read a line from a configuraiton file:
>> def _parse (self):
>>        # parse message
>>        m = self.FWShow_Command.match (conf_data)
>>        if (m == None):
>>            raise FW_Command.Error ("corrupt macro definition")
>>
>>        # store generic data
>>        macro = {}
>>        macro["key"] = m.group (1)
>>        macro["value"] = m.group (2)
>>
>>        return (conf_data, macro)
>>
>>Since there are unknown number of lines in a configuration file, I want
>>to store the array of "macro" in a list.
>>How can I do that in Python?
>>
>>Thanks
>>Sam.
> 
> Pretty sketchy description, but I'll take a shot.
> 
> fp=open(conf_data, 'r')
> macro_lines=fp.readlines()
> fp.close()
> 
> After this macro_lines will be a list with one line per element
> such that macro_list[0] is the first line, macro_list[1] the
> second, etc.
> 
Thanks for the suggestion.
Since each line of the configuration file need to be parsed into "key" 
and "value" pairs, these pair of tokens need to be stored in another 
list. Can I use the following operation to store these pair of tokens?
eg.
Assumed macro_hash["key"]="some key"
	macro_hash["value"]="some value"
then build a list that contains a list of macro_hash:
    macro_list.append(macro)
when treverse the macro_list, it would be similar to perl:
    foreach macro (in macro_list)
	key = macro["key"]
	value = macro["value"]

Sam.


> Larry Bates
> 



More information about the Python-list mailing list