I am sorry if this is a silly question but I have been working on this for hours and I cannot make it work. Please help!
I have a .txt file that originated from Excel. The file contains strings and numbers but I am only interested in the numbers, which is why I skip the first line and I only read from column 2 on.
from numpy import *
I load it into Python doing
infile = open('europenewMatrix.txt','r')
infile.readline() # skip the first line
numbers = [line.split(',')[2:] for line in infile.readlines()]
infile.close()
because I need to do computations with this, I convert it into a matrix:
travelMat = array(numbers)
ok, but this didn’t convert the strings into integers, so I manually do it:
for i in xrange(len(numbers)):
for j in xrange(len(numbers)):
travelMat[i,j] = int(self.travelMat[i,j])
#end for
At this point, I was hoping that all my entries would be integers
but if I do
print 'type is',type(self.travelMat[1,2])
the answer is:
type is <type 'numpy.string_'>
how can I really convert all my entries into integers?
thanks a lot!
convert the numbers as you read them, before creating the array: