help: output arrays into file as column

bei beiwang2003 at gmail.com
Thu Jul 27 14:52:14 EDT 2006


Hi,Simon,

Thanks for your reply.It's very helpful :)
But I am sorry for my given example.Actually, my data in the arrays are
all float point datas.And I use integer in the example.The code is like
this.
("x,v,...,h" are floating point number arrays)

pos=str(x)
vel=str(v)
ene=str(u)
den=str(rho)
pre=str(P)
hms=str(h)
datas=zip(pos,vel,ene,den,pre,hms)
filename="data.dat"
file=open(filename,"w")
for datum in datas:
  print >>file, ' '.join(datum)
file.close()


However, the result seperate each point in floating number , but not
regard them as a whole. It's like this :


[ [ [ [ [ [
- 0 1 1 1 0
0 . . . . .
. 0 4 0 0 0
5 , 9 , , 0
9   9     3
9 0 9 1 1 7
9 . 9 . . 6
9 0 9 0 0 8
9 , 9 , , 7
9   9     5
9 0 9 1 1 ,
9 . 9 . .
9 0 9 0 0 0
9 , 9 , , .
9   9     0
9 0 9 1 1 0
9 . 9 . . 3
9 0 8 0 0 7
9 , , , , 6
8         8
, 0 1 1 1 7
  . . . . 5
- 0 4 0 0 ,
0 , 9 , ,
.   9     0
5 0 9 1 1 .
9 . 9 . . 0
6 0 9 0 0 0
9 , 9 , , 3
9   9     7
2 0 9 1 1 6
4 . 9 . . 8
8 0 9 0 0 7
1 , 9 , , 5
2   9     ,
0 0 9 1 1
3 . 9 . . 0
0 0 8 0 0 .
0 , , , , 0
7         0
5 0 1 1 1 3
2 . . . . 7
, 0 4 0 0 6
  , 9 , , 8
-   9     7
0 0 9 1 1 5
. . 9 . . ,
5 0 9 0 0
9 , 9 , , 0
3   9     .
9 0 9 1 1 0
8 . 9 . . 0
4 0 9 0 0 3
9 , 9 , , 7
6   9     6
2 0 9 1 1 8
4 . 9 . . 7
0 0 8 0 0 5
6 , , , , ,
0
1 0 1 1 1 0
5 . . . . .
0 0 4 0 0 0
6 , 9 , , 0
,   9     3
  0 9 1 1 7
- . 9 . . 6
0 0 9 0 0 8
. , 9 , , 7
5   9     5
9 0 9 1 1 ,
0 . 9 . .
9 0 9 0 0 0
7 , 9 , , .
7   9     0
4 0 9 1 1 0
4 . 9 . . 3
3 0 8 0 0 7
6 , , , , 6
0         8
9 0 1 1 1 7
0 . . . . 5
2 0 4 0 0 ,
2 , 9 , ,
4   9     0
9 0 9 1 1 .
, . 9 . . 0
  0 9 0 0 0
.....

Do you have a way to work this out? Thank you very much, And sorry
again for my incorrect example.

Bei




Simon Forman wrote:
> bei wrote:
> > Hi,
> >
> > I am trying to write several arrays into one file, with one arrays in
> > one column. Each array (column) is seperated by space.
> > ie. a=[1,2,3, 4] b=[5,6,7,8] c=[9,10,11,12]
> > 1  5  9
> > 2  6  10
> > 3  7  11
> > 4  8  12
> >
> > Now I use the function file.writelines(a), file.writelines(b),
> > file.writelines(c). And the output is a sequence of strings without
> > newlines between a, b ,c . Also each array stays in row other than
> > column.
> >
> > I am a new comer to python.Any idea about this is appreciated!
> >
> > Bei
>
> Hi Bei,
>
> file.writelines() works with lists of strings, not lists of numbers, so
> I'm going to assume that a, b, and c are already lists of strings..
>
> You can use the zip() function to change the lists in the way you want
> to:
>
> |>> a=['1','2','3','4']; b=['5','6','7','8']; c=['9','10','11','12']
> |>> zip(a, b, c)
> [('1', '5', '9'), ('2', '6', '10'), ('3', '7', '11'), ('4', '8', '12')]
>
> now that you have the data for each line, you can combine them with the
> string join() method:
>
> |>> data = zip(a, b, c)
> |>> for datum in data:
> ...     print ' '.join(datum)
> ...
> 1 5 9
> 2 6 10
> 3 7 11
> 4 8 12
>
> You can print to an open file object, so the above loop will do what
> you need:
>
> |>> f = open('output.txt', 'w')
> |>> for datum in data:
> ...     print >> f, ' '.join(datum)
> ...
> |>> f.close()
> |>> print open('output.txt').read()
> 1 5 9
> 2 6 10
> 3 7 11
> 4 8 12
> 
> 
> I hope that helps!
> 
> Peace,
> ~Simon




More information about the Python-list mailing list