je recommande la fonction read_csv
de la bibliothèque pandas
:
import pandas as pd
df=pd.read_csv('myfile.csv', sep=',',header=None)
df.values
array([[ 1. , 2. , 3. ],
[ 4. , 5.5, 6. ]])
Cela donne une Pandas DataFrame - permettant 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...
Je recommande également genfromtxt
. Cependant, étant donné que la question demande une record array, par opposition à un tableau normal, le paramètre dtype=None
doit être ajouté à la genfromtxt
appel:
Compte tenu d'un fichier d'entrée, myfile.csv
:
1.0, 2, 3
4, 5.5, 6
import numpy as np
np.genfromtxt('myfile.csv',delimiter=',')
donne un tableau :
array([[ 1. , 2. , 3. ],
[ 4. , 5.5, 6. ]])
et
np.genfromtxt('myfile.csv',delimiter=',',dtype=None)
donne un tableau d'enregistrements:
array([(1.0, 2.0, 3), (4.0, 5.5, 6)],
dtype=[('f0', '<f8'), ('f1', '<f8'), ('f2', '<i4')])
Ceci a l'avantage que le fichier avec multiple data types (including strings) can be easily imported.
double possible de [Comment puis-je lire et écrire des fichiers CSV avec Python?] (Http://stackoverflow.com/questions/41585078/how -do-je-lire-et-écrire-csv-fichiers-avec-python) –