473,385 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

create/access dynamic growth list

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
3 1819
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Not Me | last post by:
Hi, I'm sure this is a common problem.. to create a single field from a whole column, where each row would be separated by a comma. I can do this for a specified table, and column.. and I've...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
4
by: Bill Sun | last post by:
Hi, All I have a conventional question, How to create a 3 dimension array by C language. I see some declare like this: int *** array; int value; array = create3Darray(m,n,l);
70
by: lgbjr | last post by:
Hello All, I've been developing a VB.NET app that requires the use of a DB. Up to now, I've been using Access. It's a bit slow, but everything works. I'm at a point now where I need to decide if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
1
by: MaryamSh | last post by:
Hi, I am creating a Dynamic Search in my application. I create a user control and in Page_load event I create a dynamic dropdownlist and 2 dynamic button (Add,Remove) By pressing Add button...
0
by: MaryamSh | last post by:
Create Dynamic Dropdownlist Controls and related event -------------------------------------------------------------------------------- Hi, I am creating a Dynamic Search in my application. I...
9
by: vanlanjl | last post by:
Okay this is very complicated (for me), but i need some help on this please. I am creating a Network Diagram in Visio, along with this I have collected all the Information for every Network Device...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.