By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

create/access dynamic growth list

P: n/a
sam
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.
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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.

Larry Bates

Jul 18 '05 #2

P: n/a
sam
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

Jul 18 '05 #3

P: n/a
sam wrote:
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"]

Why not just store the key, value pairs as actual keys and values of a map? Creating a list of maps
seems a bit contorted here. Instead of macro_hash["key"]="some key"
macro_hash["value"]="some value"


followed by creating a list of macro_hashes, just use
macro_hash["some key"] = "some value"

You lose the order of values but gain much easier lookup.

Alternatively just create a list of (key, value) pairs with
macro_list = []
macro_list.append( ("some key", "some value") )

which preserves the order at the expense of easy random access.

Kent
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.