mark mcmurray
mark mcmurray

Reputation: 1671

Creating a numpy array from a .csv file but I can only get the 1st line in the array - what am I missing?

I have data in a file which looks like this

0.5,0,-21
0.5,0,-21
0.5,0,-19
0.5,0,-20
0.5,0,-20
1,0,-31
1,0,-28
1,0,-31
1,0,-28
1,0,-30

And I want to create a numpy array. I am doing the following to try copy it into an array:

with open ("bot1.csv") as fd:
    array = numpy.fromfile(fd, count=-1, dtype=float, sep=",")

But the resulting array is just:

array([  0.5,   0. , -21. ])

Any idea what I am doing wrong?

Upvotes: 1

Views: 1508

Answers (1)

Eric O. Lebigot
Eric O. Lebigot

Reputation: 94485

I would suggest that you use numpy.loadtxt(). It is faster than genfromtxt()—but less flexible, which should not matter in your case:

table = numpy.loadtxt('bot1.csv', delimiter=',')

Side note: it is best to not call you variable array, as this is also a name used by NumPy: this makes the code less convenient to paste after doing from numpy import * or from pylab import * (in a Python shell), because people can expect array to mean numpy.array(), and because the variable name shadows NumPy's array().

Upvotes: 4

Related Questions