From: Francesc A. <fa...@ca...> - 2006-11-08 14:45:26
|
A Dimecres 08 Novembre 2006 13:42, amit soni escrigu=C3=A9: > 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=3D[1 2;3 9] > B=3D[2 3;4 4] > > Can anyone tell me how to do this in python? > thanks > Amit There are many possibilities. One of them could be: In [64]: a =3D []; b =3D [] In [65]: for i, line in enumerate(file("/tmp/data.txt")): ....: if i < 2: ....: a.extend([float(n) for n in line.split()]) ....: else: ....: b.extend([float(n) for n in line.split()]) ....: In [66]: A=3Dnumpy.array(a).reshape(2,2); B=3Dnumpy.array(b).reshape(2,2) In [67]: A, B Out[67]: (array([[ 1., 2.], [ 3., 9.]]), array([[ 2., 3.], [ 4., 4.]])) HTH, =2D-=20 >0,0< Francesc Altet =C2=A0 =C2=A0 http://www.carabos.com/ V V C=C3=A1rabos Coop. V. =C2=A0=C2=A0Enjoy Data "-" |