user3125347 - 6 months ago 39

Python Question

I have a csv file ready to load into my python code, however, I want to load it into the following format:

`data = [[A,B,C,D],`

[A,B,C,D],

[A,B,C,D],

]

How would I go about loading a .csv file that is readable as a numpy array? e.g., simply using previous tutorials plays havoc with using:

`data = np.array(data)`

Failing that, I would just like to upload my csv file (e.g. 'dual-Cored.csv' as data = dual-Cored.csv)

Answer

The simplest solution is just:

```
import numpy as np
data = np.loadtxt("myfile.csv")
```

As long as the data is convertible into `float`

and has an equal number of columns on each row, this works.

If the data is not convertible into `float`

in some column, you may write your own converters for it. Please see the `numpy.loadtxt`

documentation. It is really very flexible.