This seems like a very simple process, but I cannot get the data out of the loop:
I have a data file "slink.txt":
88
10112213
33332332
22011220
23110122
21231102
12222321
32131132
10023232
I am trying to store the numbers in a 2 dimensional array:
f = open('slink.txt', 'r')
rows = int(f.read(1))
cols = int(f.read(1))
f.read(1) #skip endline
temp = []
data = []
for row in range(rows):
for col in range(cols):
temp.append(int(f.read(1)))
f.read(1)
data.append(temp)
print '\n\nRow:', row, temp, '\nData: ', data #debug
del temp[:]
print '\n\n\n',data
A sample of my output shows:
Row: 0 [1, 0, 1, 1, 2, 2, 1, 3]
Data: [[1, 0, 1, 1, 2, 2, 1, 3]]
Row: 1 [3, 3, 3, 3, 2, 3, 3, 2]
Data: [[3, 3, 3, 3, 2, 3, 3, 2], [3, 3, 3, 3, 2, 3, 3, 2]]
Row: 2 [2, 2, 0, 1, 1, 2, 2, 0]
Data: [[2, 2, 0, 1, 1, 2, 2, 0], [2, 2, 0, 1, 1, 2, 2, 0], [2, 2, 0, 1, 1, 2, 2, 0]]
.
.
.
[[], [], [], [], [], [], [], []]
The last line shows the data variable to be empty. Something doesn't seem right. Please help.
Ben