[Numpy-discussion] genfromtxt behaviour

Matt Studley matt.studley@gmail....
Fri Oct 29 07:35:20 CDT 2010


Hi all

first, please forgive me for my ignorance - I am taking my first
stumbling steps with numpy and scipy.

I am having some difficulty with the behaviour of genfromtxt.

s = SIO.StringIO("""1, 2, 3
4, 5, 6
7, 8, 9""")
g= genfromtxt(s, delimiter=', ', dtype=None)
print g[:,0]


This produces the output I expected, a slice for column 0...

array([1, 4, 7])


BUT....

s = SIO.StringIO("""a, 2, 3
b, 5, 6
c, 8, 9""")
g= genfromtxt(s, delimiter=', ', dtype=None)
g[:,0]

Produces the following error:

IndexError: invalid index


....

In the first case, genfromtxt returns me a 2d array (list of lists),
in the second it returns a list of tuples with an associated dtype
list of tuples.

How can I do my nice 2d slicing on the latter?

array([('a', 2, 3), ('b', 5, 6), ('c', 8, 9)],
      dtype=[('f0', '|S1'), ('f1', '<i4'), ('f2', '<i4')])

thanks in advance for any assistance

Matt


More information about the NumPy-Discussion mailing list