matrix file format
Hi, I'm trying to find out what format a matrix input file should have in numpy. example : I have the matrix : [[1 2 3 4 5 6 7 8 9]] and want to read it to nympy from a text-filen that is easy for a user to generate in an editor. Can you tell me how to do that. I read the manual and it didnøt given much af on idea. I thank you in advance Peter I. Hansen
In principle there is no such format in the sense of other numerical environments. But it is possible to simple put the data into a python module and import that, like # Datafile data.py import Numeric data=Numeric.array([[1,2,3],[4,5,6],[7,8,9]]) # in python (other module or interactive session) from data import data print data HTH __Janko Peter I. Hansen writes:
Hi,
I'm trying to find out what format a matrix input file should have in numpy. example :
I have the matrix : [[1 2 3 4 5 6 7 8 9]]
and want to read it to nympy from a text-filen that is easy for a user to generate in an editor.
Can you tell me how to do that. I read the manual and it didnøt given much af on idea.
I thank you in advance
Peter I. Hansen
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net http://lists.sourceforge.net/mailman/listinfo/numpy-discussion
You have many options: I frequently use Konrad Hinsen's "Scientific" package (http://dirac.cnrs-orleans.fr/programs/scientific.html). It make importing files from user input or other packages like matlab easy and supports several file formats. The simpliest is a basic white-space-delimitted text file if you have a text file "arrayfile.txt" that looks like this: # arrayfile.txt (lines beginning with '#' are ignored) 1 2 3 4 5 6 6 8 9 You can fire up python and read in your matrix like this:
from Scientific.IO.ArrayIO import readArray A = readArray('arrayfile.txt') print A [[1 2 3] [4 5 6] [6 8 9]]
I'm trying to find out what format a matrix input file should have in numpy.
Any format you like, but you have to supply the I/O code yourself. Which however is really simple. For example, the following function from ScientificPython: from Scientific.IO.TextFile import TextFile import string, Numeric def readArray(filename): "Return an array containing the data from file |filename|." data = [] for line in TextFile(filename): if line[0] != '#': data.append(map(eval, string.split(line))) a = Numeric.array(data) if a.shape[0] == 1 or a.shape[1] == 1: a = Numeric.ravel(a) return a Konrad. -- ------------------------------------------------------------------------------- Konrad Hinsen | E-Mail: hinsen@cnrs-orleans.fr Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.56.24 Rue Charles Sadron | Fax: +33-2.38.63.15.17 45071 Orleans Cedex 2 | Deutsch/Esperanto/English/ France | Nederlands/Francais -------------------------------------------------------------------------------
participants (4)
-
Christopher Lee
-
Janko Hauser
-
Konrad Hinsen
-
Peter I. Hansen