[Numpy-discussion] using loadtxt to load a text file in to a numpy array
josef.pktd at gmail.com
josef.pktd at gmail.com
Fri Jan 17 21:15:51 EST 2014
It looks like both recfromtxt and loadtxt are flexible enough to
handle string/bytes en/decoding, - with a bit of work and using enough
information
>>> dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<U9')]
>>> data = numpy.recfromtxt(open('Õscar_3.txt',"rb"), dtype=dtype, delimiter=',',converters={3:lambda x: x.decode('utf8')})
>>> data['f3'] == 'Õscar'
array([False, False, False, True], dtype=bool)
>>> data
rec.array([(1, 2, 3, 'hello'), (5, 6, 7, 'Õscarscar'), (15, 2, 3, 'hello'),
(20, 2, 3, 'Õscar')],
dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<U9')])
>>> data = numpy.loadtxt(open('Õscar_3.txt',"rb"), dtype=dtype, delimiter=',',converters={3:lambda x: x.decode('utf8')})
>>> data
array([(1, 2, 3, 'hello'), (5, 6, 7, 'Õscarscar'), (15, 2, 3, 'hello'),
(20, 2, 3, 'Õscar')],
dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4'), ('f3', '<U9')])
>>>
Josef
More information about the NumPy-Discussion
mailing list