Search code examples
python-3.xpandascsvgenfromtxt

genfromtxt return numpy array not separated by comma


I have a *.csv file that store two columns of float data. I am using this function to import it but it generates the data not separated with comma.

data=np.genfromtxt("data.csv", delimiter=',', dtype=float)

output:

[[ 403.14915    150.560364 ]
 [ 403.7822265  135.13165  ]
 [ 404.5017     163.4669   ]
 [ 434.02465    168.023224 ]
 [ 373.7655     177.904114 ]
 [ 450.608429   208.4187315]
 [ 454.39475    239.9666595]
 [ 453.8055     248.4082   ]
 [ 457.5625305  247.70315  ]
 [ 451.729431   258.19335  ]
 [ 366.74405    225.169922 ]
 [ 377.0055235  258.110077 ]
 [ 380.3581     261.760071 ]
 [ 383.98615    262.33805  ]
 [ 388.2516785  272.715332 ]
 [ 408.378174   200.9713135]]

How to format it to get a numpy array like

[[ 403.14915,    150.560364 ]
     [ 403.7822265,  135.13165  ],....]

?


Solution

  • NumPy doesn't display commas when you print arrays. If you really want to see them, you can use

    print(repr(data))
    

    The repr function forces a str representation not ment for "nice" printing, but for the literal representation you would use yourself to type the data in your code.