Basically what I want is, if I have a file that looks like:

1 2

3 4

I want to read that in where list[0][0] = 1, list[0][1] = 2, list[1][0] = 3 and list[1][1] = 4 (i may have those indices switched, its been a while but that doesnt matter). How can I do this, where the numbers in the target file are seperated by spaces?

First I tried:

- Code: Select all
`f = open('file.txt', 'r')`

for line in f:

l.append(line)

but that doesnt work cause you can't append to a 2d array. Then I tried:

- Code: Select all
`with open('file.txt','r') as f:`

f = open('file.txt', 'r')

for line in f.readlines():

a = line.split(' ')

l.append(a)

where l is defined as: l=np.zeros(shape=(15,15))

I tried the last one at the suggestion of a friend but it would even compile. I also tried the method:

datain = np.loadtxt('file.txt')

and all that did was append to the list the first element of each line.

What am I doing wrong and how can I read this data in to create a list like the one I want? Thanks in advance for any help.

- Bill