[Tutor] reading matrixes from a file
Silviu Cojocaru
silviucc@home.ro
Thu Jul 10 12:47:10 2003
I'm trying to read matrixes from a given file. I have writen three
functions for this:
# this will be used to initialize a matrix
def initMatrix(matrix_,rows_):
matrix_ = ''
matrix_ = list(matrix_)
for i in range(rows_):
null = ' '
null = list(null)
matrix_ = matrix_ + null
return matrix_
# this one does the actual readinf from the file
def readMatrix(filename):
f = file(filename,'r')
cr = f.readline()
rows = int(cr[0])
matrix =''
matrix = initMatrix(matrix,rows)
matrix = list(matrix)
for i in range(rows):
line = f.readline()
line2 = ''
line2 = list(line2)
for j in range(len(line)):
if line[j] == ' ' or line[j] == '\n' or line[j]
== '\r': continue
else:
line2 = line2 + list(line[j])
matrix[i] = list(matrix[i]) + line2
del(matrix[i][0])
f.close()
return matrix
# I use this one to convert, for example, '1' to 1 or '0' to 0
def intMatrix(matrix):
for row in range(len(matrix)):
for col in range(len(matrix[row])):
matrix[row][col] = int(matrix[row][col])
return matrix
The problem:
These functions work well for this file:
http://www.computing.dcu.ie/~cdaly/ire_comp/ire01/problems/test2/test1.dat
but not for this one:
http://www.computing.dcu.ie/~cdaly/ire_comp/ire01/problems/test2/test8.dat
For this one I get only the first line. I have triple checked the
function and I have no clue to what may be wrong.
Please help me :)
--
Registered Linux user #298569
QOTD:
"Do you smell something burning or is it me?"
-- Joan of Arc