Cecilia Guerra - 11 months ago 30

Python Question

I have a *cvs* file which has three columns of numbers up to three digits each:

`1 0 0`

2 0 0

3 0 0

4 0 0

5 0 0

6 0 0

7 0 0

8 0 0

9 0 0

10 0 0

11 0 0

I want to read the columns separately and be able to use them as arrays with:

`data = np.loadtxt('file.csv')`

x = data[:, 0]

y = data[:, 1]

But I'm getting:

`X = np.array(X, dtype)`

ValueError: setting an array element with a sequence.

If instead I use the line

`x,y = np.loadtxt('Beamprofile.txt', usecols=(0,1), unpack=True)`

Answer Source

With your sample:

```
In [1]: data=np.loadtxt('stack39174768.txt')
In [2]: data
Out[2]:
array([[ 1., 0., 0.],
[ 2., 0., 0.],
[ 3., 0., 0.],
[ 4., 0., 0.],
[ 5., 0., 0.],
[ 6., 0., 0.],
[ 7., 0., 0.],
[ 8., 0., 0.],
[ 9., 0., 0.],
[ 10., 0., 0.],
[ 11., 0., 0.]])
In [3]: x=data[:,0]
In [4]: y=data[:,1]
In [5]: x
Out[5]: array([ 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.])
In [6]: y
Out[6]: array([ 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
```

Where's the problem? Is there's something about the file that we're not seeing in the clip?

```
In [8]: x,y = np.loadtxt('stack39174768.txt', usecols=(0,1), unpack=True)
```

produces the same `x`

and `y`

.

I asked about shape and dtype

```
In [11]: data.shape
Out[11]: (11, 3)
In [12]: data.dtype
Out[12]: dtype('float64')
```

I like `np.genfromtxt`

a little better, but in this case it produces the same `data`

.