[Tutor] The concept of string arrays

Don Arnold Don Arnold" <darnold02@sprynet.com
Mon Feb 3 21:21:14 2003


----- Original Message -----
From: "andy surany" <mongo57a@comcast.net>
To: <tutor@python.org>
Sent: Monday, February 03, 2003 6:30 PM
Subject: [Tutor] The concept of string arrays


> Hello Members!
>
> I'm reading an Excel file into memory. The values are all strings. My no
> frills approach to doing this was to simply set up a list, read the
> values from a file, and append the list. Each element is then referenced
> via position. For example,
>
> First_Name    Last_Name    Address    Date_of_Birth
>
> would be input as follows:
>
> client.append(First_Name)
> client.append(Last_Name)
> client.append(Address)
> client.append(Fate_of_Birth)
>
> now if I want to reference the 45th row in the cell, I would start at
> position 45*4.
>
> This works - but I know that there have to be more elegant solutions. I
> tried creating a 4 dim array - but found that string types were not
> allowed. Is it possible to create a multi dimensional list or tuple?
> or????
>
> TIA.
>
> Andy
>

I'm not sure this is what you're shooting for, but it might be a step in the
right direction:

class Holder:
    def __init__(self,headers,data):
        for key, value in zip(headers,data):
            self.__dict__[key] = value

    def __getitem__(self,key):
        return self.__dict__[key]


## assumes you've already read in the first line (which contains the column
## names) and converted it via one of the CSV modules into a list of strings

headerValues = ['First_Name','Last_Name','Address','Date_of_Birth']

## same thing with current spreadsheet row

dataLine = ['Don','Arnold','12345 Main St','1967-02-21']

client = Holder(headerValues, dataLine)

for key, value in client.__dict__.items():
    print "client['%s'] = %s" % (key, client[key])

---- begin script output ----
client['First_Name'] = Don
client['Last_Name'] = Arnold
client['Date_of_Birth'] = 1967-02-21
client['Address'] = 12345 Main St
---- end script output ----

As you can see, you are now able to access a specific field in the row by
column name, instead of  column position. This is especially useful if
columns get swapped around: you don't care where the column for the first
name is, as long as it's called First_Name.

You can build a list of these Holder instances as you would any other list,
and still access each ones fields as before:


clientList = []
fileLines = [
    ['John','Smith','1 Grand Ave','1930-01-01'],
    ['Bob','Jones','5 Willow Pk','1925-03-17'],
    ['Susan','Smith','4 Milky Way','1980-07-04']
    ]

for dataLine in fileLines:
    clientList.append(Holder(headerValues,dataLine))

print clientList[0]['First_Name']
print clientList[1]['Last_Name']
print clientList[2]['Address']

---- begin script output ----
John
Jones
4 Milky Way

---- end script output ----

HTH,
Don