Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Given a .csv file that look like this:

49.18;-3.34
45.73;2.63
47.88;-11.49
45.74;3.06
48.73;-9.56
49.20;-3.42

I use the following code:

data = np.genfromtxt("file.csv",  delimiter=';')

And the resulting array looks like this:

[[ 49.18  -3.34]
 [ 45.73   2.63]
 [ 47.88 -11.49]
 [ 45.74   3.06]
 [ 48.73  -9.56]
 [ 49.2   -3.42]
 [ 45.71   2.53]
 [ 47.87 -11.67]]

But what I want is this:

 [[ 49.18 , -3.34]
 [ 45.73 ,  2.63]
 [ 47.88 , -11.49]
 [ 45.74 ,  3.06]
 [ 48.73 , -9.56]
 [ 49.2  , -3.42]
 [ 45.71 ,  2.53]
 [ 47.87 , -11.67]]

What am I doing wrong? Thanks a lot, and I hope you can help me

share|improve this question
3  
Everything is fine. The print form of numpy doesn't shows the comma –  wolfrevo Feb 6 at 12:27

2 Answers 2

up vote 0 down vote accepted

What is the output of

data.shape

I believe it is already working as you wish.

share|improve this answer
    
This is the output: (125L, 2L) –  user3626454 Feb 6 at 12:33
    
So, 125 rows by 2 columns? Is the problem that you want it to print a certain way or that you want it in a numpy array to work with? Is type(data) an numpy.ndarray? –  EngineeredE Feb 6 at 12:40
    
That's right, it's 125 rows and 2 columns. The problem is that I cannot access the columns separately using indexes. It is a numpy.ndarray –  user3626454 Feb 6 at 12:48
    
Please provide an example the indexing and the error it produces. data[:,0] should access the first column –  EngineeredE Feb 6 at 12:49
    
Thanks, it was the indexes what I was doing wrong! –  user3626454 Feb 6 at 13:03

If you really need an array instead of a numpy-array, then you can convert your np.array to a list with tolist():

my_list = data.tolist()

Please remember that with a list you only can index every dimension with following notation:

print my_list[0][0]

and with a numpy-array you can use both notations:

print data[0,0]
print data[0][0]
share|improve this answer
    
tolist() produces a list, not an array. Or rather a list of lists. Python has a separate class called array. But with the wide availability of numpy, that is rarely used. I know I'm being picky, but terminology does matter when programming. –  hpaulj Feb 6 at 17:22
    
You're absolutely right. Thanks for you comment. –  wolfrevo Feb 7 at 15:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.