tags:

views:

367

answers:

2

hi all, how can I import an array to python (numpy) from a file and that way the file must be written. For example, a matrix

to and from that file type (extention).

thanks for any response

+5  A: 

Checkout the entry on the numpy example list. Here is the entry on .loadtxt()

>>> from numpy import *
>>>
>>> data = loadtxt("myfile.txt")                       # myfile.txt contains 4 columns of numbers
>>> t,z = data[:,0], data[:,3]                         # data is 2D numpy array
>>>
>>> t,x,y,z = loadtxt("myfile.txt", unpack=True)                  # to unpack all columns
>>> t,z = loadtxt("myfile.txt", usecols = (0,3), unpack=True)     # to select just a few columns
>>> data = loadtxt("myfile.txt", skiprows = 7)                    # to skip 7 rows from top of file
>>> data = loadtxt("myfile.txt", comments = '!')                  # use '!' as comment char instead of '#'
>>> data = loadtxt("myfile.txt", delimiter=';')                   # use ';' as column separator instead of whitespace
>>> data = loadtxt("myfile.txt", dtype = int)                     # file contains integers instead of floats
bayer
hello, thanks for answering, I can only doubt as he defines the path of where the file
ricardo
+1  A: 

Have a look at SciPy cookbook. It should give you an idea of some basic methods to import /export data.

If you save/load the files from your own Python programs, you may also want to consider the Pickle module, or cPickle.

extraneon
Pickling is inappropriate for arrays - while you can do it, it will be slow as hell. Use np.save() to save in the .npy format or np.savez() to save a zipped archive of several arrays.
dwf