Say I have a file myfile.txt
containing:
1 2.0000 buckle_my_shoe
3 4.0000 margery_door
How do I import data from the file to a numpy array as an int, float and string?
I am aiming to get:
array([[1,2.0000,"buckle_my_shoe"],
[3,4.0000,"margery_door"]])
I've been playing around with the following to no avail:
a = numpy.loadtxt('myfile.txt',dtype=(numpy.int_,numpy.float_,numpy.string_))
EDIT: Another approach might be to use the ndarray type and convert afterwards.
b = numpy.loadtxt('myfile.txt',dtype=numpy.ndarray)
array([['1', '2.0000', 'buckle_my_shoe'],
['3', '4.0000', 'margery_door']], dtype=object)
\n
newline and explode those inners with the 3- and 2 spaces. Otherwise you can also just use Regular Expressions to find each lines and split them up (groups). – user1467267 Mar 18 '13 at 16:18numpy
to work with non-numerical data, if you want to do anything fun with it you're probably going to wind up reinventing bits ofpandas
.. – DSM Mar 18 '13 at 16:24