lo consiglio la funzione read_csv
dalla libreria pandas
:
import pandas as pd
df=pd.read_csv('myfile.csv', sep=',',header=None)
df.values
array([[ 1. , 2. , 3. ],
[ 4. , 5.5, 6. ]])
Questo dà un panda DataFrame - permettendo many useful data manipulation functions which are not directly available with numpy record arrays.
DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table...
consiglierei anche genfromtxt
. Tuttavia, poiché la domanda chiede un record array, al contrario di una matrice normale, il parametro dtype=None
deve essere aggiunto al genfromtxt
chiamata:
Dato un file di input, myfile.csv
:
1.0, 2, 3
4, 5.5, 6
import numpy as np
np.genfromtxt('myfile.csv',delimiter=',')
dà un array :
array([[ 1. , 2. , 3. ],
[ 4. , 5.5, 6. ]])
e
np.genfromtxt('myfile.csv',delimiter=',',dtype=None)
dà una matrice di record:
array([(1.0, 2.0, 3), (4.0, 5.5, 6)],
dtype=[('f0', '<f8'), ('f1', '<f8'), ('f2', '<i4')])
Questo ha il vantaggio che il file con multiple data types (including strings) can be easily imported.
Eventuali duplicati di [Come faccio a leggere e scrivere file CSV con Python?] (Http://stackoverflow.com/questions/41585078/how -do-i-read-and-write-csv-files-with-python) –