FW: [Tutor] inquire

Doug Stanfield DOUGS@oceanic.com
Thu, 11 Jan 2001 09:33:23 -1000


Forwarding this to the list to expedite more immediate help:
> 
> If there is a table of data like this:
> 
> record	|name	|gender	|age	|......
> 1	|'Jim'	|m	|23	|.....
> 2	|'Mary'	|f	|19	|.....
> 
> I don't know the structure of the table when I write my 
> program, so I have to dynamicsly design a class object to 
> simulate a record. I wish the properties of my class have the 
> name same to the table. How can I do it? 
> The matter is actually that, if I have string value, how can 
> I generate a variable with the string as its name?

This reminds me of a Python dictionary.  That would probably be the easiest
route to finding a solution.  For example I've done this before:

    tm = statcmd.readline()[:-1]
    heads = string.split(statcmd.readline()[:-1],",")
    heads[0] = 'IP'
    values = string.split(statcmd.readline()[:-1],",")
    tHeads = heads[0:16]
    tValues = values[0:16]
    stats = {}
    for key,value in map(None,tHeads,tValues):
        stats[key] = value
    stats['time'] = tm

Where the format of the file I was reading was like this:

Thu Jan 11 09:26:05 HST 2001
IP Addr,Name,Location,UpTime,ByteRx,ByteTx
10.254.91.57,139322038,8100lana1,...  etc.

In other words, the first line of the file has a date stamp which I put in
the tm variable.  Later I associate that with the dictionary key 'time'.
The next line has the header of variable names.  I put that in a list called
heads.  The next line has values associated with those headings, put into
the list called variables.  After some initialization I use the for loop to
populate the dictionary called stats with key value pairs where the key is
the header and the value is the corresponding value from the second line.

With a little extra code this could be adapted to your multiple record line
file.

Good luck.

-Doug-