reading matrix from a file

Charles R Harris charlesr.harris at gmail.com
Wed Nov 8 08:55:41 CST 2006


On 11/8/06, Francesc Altet <faltet at carabos.com> wrote:
>
> A Dimecres 08 Novembre 2006 13:42, amit soni escrigué:
> > Hi,
> >   i have a file with following format:
> >  1 2
> >  3 9
> >  2 3
> >  4 4
> >    I want to read it and then store the values into two matrices, s.t.
> >  A=[1 2;3 9]
> >  B=[2 3;4 4]
> >
> >   Can anyone tell me how to do this in python?
> >  thanks
> >  Amit


Try

In [8]: tmp = fromfile('tmp.txt', sep=' ', dtype=int)

In [9]: a = tmp[:4].reshape(2,2)

In [10]: b = tmp[4:].reshape(2,2)

In [11]: a
Out[11]:
array([[1, 2],
       [3, 9]])

In [12]: b
Out[12]:
array([[2, 3],
       [4, 4]])

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061108/fe8dbf12/attachment-0001.html 
-------------- next part --------------
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
-------------- next part --------------
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion


More information about the Numpy-discussion mailing list