0

This question maybe asked earlier (atleast topic wise) , but still I couldn't find a solution for my specific problem. Basically, I need a multidimensional array in python. Such that:

I will be able to access contents in list by :

contenets[no_of_record][record]. 

So I have a file like :

101, Mrs. Jones' Math Class, 10100000001, Jones, Barbara, , , , 10100000011, Gutierrez, Kimberly, 2
101, Mrs. Jones' Math Class, 10100000001, Jones, Barbara, , , , 10100000013, Mercado, Toby, 1
101, Mrs. Jones' Math Class, 10100000001, Jones, Barbara, , , , 10100000014, Garcia, Lizzie, 1
101, Mrs. Jones' Math Class, 10100000001, Jones, Barbara, , , , 10100000015, Cruz, Alex, 1

Now I have to maintain a multi dimensional array where no_of_record points to row number and the record will be the column number.

I want something similar to this:

contents=[][]

for lines in source_file:
                contents[no_of_records][]=lines.rstrip().split(',')
                print contents[no_of_records]
                no_of_records+=1

I am sure the snippet above is syntactically wrong, I am just trying to give an idea of what im searching for. Thanks for your help.

-Sethu

1
  • 3
    i'd suggest csv Commented Sep 20, 2011 at 16:08

4 Answers 4

4

from the csv examples of the doc you should do something along the lines

import csv
rows = []
with open('some.csv', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        print row
        rows.append(row)
Sign up to request clarification or add additional context in comments.

Comments

3

If you really want a list of lists you can do it with this:

contents = [l.split(',') for l in fh] # fh is a file handle here.

There's no good reason not to use the csv module if you're actually working with a csv file though.

1 Comment

It is a csv file. Im surprised I didnt search for an inbuilt csv library. Thanks for mentioning it !!
1

You can also do it with dicts, which may be better, if you want to use something other than numbers for the indices.

contents = []
tmp_rcd = {'classnum': 101, 'classname': "Mrs. Jones' Math Class", ...}
contents.append.tmp.rcd
...
print contents[i]['classname']

2 Comments

Dictionary sounds perfect. I just have to find out how to use it. I have never used them before.
If you want to combine this with csv, there's csv.DictReader, which can take the field names from the first row, or you can specify them yourself.
1

Use a single list, but populate it further using list objects.

contents = []
for i, line in enumerate(source_file):
    contents.append(line.rstrip().split(','))
    print contents[i]

It looks like you're parsing a csv file, though. I'd suggest using the csv module in stead.

2 Comments

You can't assign to an index that doesn't exist in a list. It raises IndexError.
@g.d.d.c Realized that after I read over my answer again. Edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.