-4

I have a csv file that contains a list of data points.

1.3
5.0
15.35
14.3
9.8
4.4
7.6
9.87

How can I write a python program that can read the numbers in the csv file and make them into an array x so that:

x=[1.3,5.0,15.35,14.3,9.8,4.4,7.6,9.87]

?

jms1980
  • 785
  • 1
  • 12
  • 31

2 Answers2

0

You can use inbuilt genfromtext method from numpy.
More info on numpy.genfromtext is here

Demo Code:

import numpy as np
x= np.genfromtxt ('youData.csv', delimiter=",")
print x

Output:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
[  1.3    5.    15.35  14.3    9.8    4.4    7.6    9.87]
>>> 
Anil_M
  • 8,692
  • 2
  • 32
  • 59
0

Or the easiest solution in my experience and fastest CSV reading:

import pandas as pd
import numpy as np
x=pd.read_csv('file.csv', header=None)
x=np.array(x.values)
Matt
  • 1,995
  • 9
  • 28
  • You will lose the first value `1.3`, you would need to do `x = pd.read_csv('file.csv', header=None)` – Akavall Apr 05 '16 at 19:35