0

I have some very simple data I created that I saved in a csv file to test :

1,1.8
2,4.5
3,9.6
4,16
5,25.3

And I want to import them in Python. In IDLE this works as expected using genfromtxt from the numpy package:

>>> my_data = genfromtxt('very_simple_data.csv', delimiter=',')
>>> my_data
     array([[ 1. ,  1.8],
            [ 2. ,  4.5],
            [ 3. ,  9.6],
            [ 4. , 16. ],
            [ 5. , 25.3]])

But the same code cut n pasted into a Jupyter notebook doesn't work:

 In[23]: my_data = genfromtxt('very_simple_data.csv', delimiter=',')
 In[24]: my_data
 Out[24]: array([  1. ,   nan,   nan,   nan,   nan,  25.3])

So there are two problems here: first some values are not being stored as numbers and I do not have a 2 dimensional array.

A more basic implementation in Jupyter does work:

 In[25]: with open('very_simple_data.csv', 'r') as f:
              reader = csv.reader(f)
              new_data = list(reader)
 In[26]: new_data
 Out[26]: [['1', '1.8'], ['2', '4.5'], ['3', '9.6'], ['4', '16'], ['5', '25.3']]

The only thing I can think of is the numpy package on my machine is v 1.14.0 and the one associated with Jupyter is a bit older: 1.11.3 (which I gather you are 'stuck' with when using Jupyter).

Any ideas on what is going on? Or is Jupyter not considered that stable?

DJBunk
  • 101
  • 1
  • 3
  • I suspect this has something to do with the line endings in the CSV file. In the case where it doesn't work, it looks like the line endings are not being interpreted correctly, and `genfromtxt` thinks the file is one long line. – Warren Weckesser Jan 10 '18 at 19:06
  • As per @WarrenWeckesser's comment, if I prune the csv file by hand in a text editor (deleting white space and then hitting enter to restore it) genfromtxt within Jupyter works fine. The question still remains to some extend that genfromtxt works differently in Jupyter in some cases. – DJBunk Jan 10 '18 at 19:28
  • The issue seems to be that Exel for Mac doesn't have the current standard for line endings, see: https://nicercode.github.io/blog/2013-04-30-excel-and-line-endings/ so again the issue isn't directly a Python one, but there is still the issue that IDLE and Jupyter seem to handle the issue differently. – DJBunk Jan 10 '18 at 19:51

0 Answers0