I am writing a method to create an array from a data file. The method looks like this:
import numpy def readDataFile(fileName): try: with open(fileName, 'r') as inputs: data = None for line in inputs: line = line.strip() items = line.split('\t') if data == None: data = numpy.array(items[0:len(items)]) else: data = numpy.vstack((data, items[0:len(items)])) return numpy.array(data) except IOError as ioerr: print 'IOError: ', ioerr return None
My data file contains strings of numbers, each of which is separated by a tab, for example:
1 2 3 4 5 6 7 8 9
And I expect to get an array as follows:
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
However, the result contains dtype
at the end of this word:
array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype='|S9')
Because of this, I cannot perform some operations on the result, for example. if I try to find the maximum value for each row using result.max(0)
, I get an error:
TypeError: cannot perform reduction using a flexible type.
So, can someone tell me what is wrong with my code and how to fix it? Many thanks.