turnip
turnip

Reputation: 2346

Creating an array using data from a text file (NumPy)

I am trying to create an array with 2 columns and over 7000 rows (lots of data). The data I have is written as a text file and is formatted in two columns, each variable being separated by a space. My biggest problem is that NumPy can't seem to find the file.

 1.  import numpy as np
 2.  np.fromfile(stardata.txt)

This returns:

NameError: name 'stardata' is not defined

I have checked the directory and everything seems to be in order. The file is in the correct directory.

My next problem is seeing if this would make a good array. I am guessing I might have to use .reshape() to make it look the way I want it to be. After that, I will be using the data to make a (Hertzsprung-Russell) diagram.

The full error message is here:

C:\Users\Petar\AppData\Local\Enthought\Canopy\System\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
    174             else:
    175                 filename = fname
--> 176             exec compile(scripttext, filename, 'exec') in glob, loc
    177     else:
    178         def execfile(fname, *where):

    C:\Users\Petar\Desktop\test.py in <module>()
          1 import numpy as np
    ----> 2 np.fromfile(stardata.txt)

    NameError: name 'stardata' is not defined

Upvotes: 1

Views: 1347

Answers (1)

Andy
Andy

Reputation: 50560

Your file name needs to be wrapped in quotes:

np.fromfile('stardata.txt')

Upvotes: 3

Related Questions