Re: help: output arrays into file as column

2006-07-28 Thread bei
Perfect! It works. Thanks Bruno.
Bruno Desthuilliers wrote:
> bei a écrit :
>  Please don't top-post
>
> > 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)
>
> Arrays or lists ?
>
> > pos=str(x)
>
> Why on earth are you doing this ?
>
> > vel=str(v)
> > ene=str(u)
> > den=str(rho)
> > pre=str(P)
> > hms=str(h)
> > datas=zip(pos,vel,ene,den,pre,hms)
>
> datas = zip(v, u, rho, P, h)
>
> > filename="data.dat"
> > file=open(filename,"w")
>
> This shadows the builtin 'file' type. Using another name may be a good idea.
>
> > for datum in datas:
> >   print >>file, ' '.join(datum)
>print >> file, ' '.join(map(str, 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
>
> (snip)
>
> One of the nice things with Python is the interactive shell. Let's use it:
> Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
> [GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on
> linux2
> Type "help", "copyright", "credits" or "license" for more information.
>  >>> a = map(float, range(0,3))
>  >>> a
> [0.0, 1.0, 2.0]
>  >>> b = map(float, range(12, 15))
>  >>> c = map(float, range(7, 10))
>  >>> a, b, c
> ([0.0, 1.0, 2.0], [12.0, 13.0, 14.0], [7.0, 8.0, 9.0])
>  >>> str(a)
> '[0.0, 1.0, 2.0]'
>  >>> list(str(a))
> ['[', '0', '.', '0', ',', ' ', '1', '.', '0', ',', ' ', '2', '.', '0', ']']
>  >>> zip(str(a), str(b), str(c))
> [('[', '[', '['), ('0', '1', '7'), ('.', '2', '.'), ('0', '.', '0'),
> (',', '0', ','), (' ', ',', ' '), ('1', ' ', '8'), ('.', '1', '.'),
> ('0', '3', '0'), (',', '.', ','), (' ', '0', ' '), ('2', ',', '9'),
> ('.', ' ', '.'), ('0', '1', '0'), (']', '4', ']')]
>  >>> zip(a, b, c)
> [(0.0, 12.0, 7.0), (1.0, 13.0, 8.0), (2.0, 14.0, 9.0)]
>  >>>
> 
> HTH

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help: output arrays into file as column

2006-07-27 Thread Bruno Desthuilliers
bei a écrit :
 Please don't top-post

> 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)

Arrays or lists ?

> pos=str(x)

Why on earth are you doing this ?

> vel=str(v)
> ene=str(u)
> den=str(rho)
> pre=str(P)
> hms=str(h)
> datas=zip(pos,vel,ene,den,pre,hms)

datas = zip(v, u, rho, P, h)

> filename="data.dat"
> file=open(filename,"w")

This shadows the builtin 'file' type. Using another name may be a good idea.

> for datum in datas:
>   print >>file, ' '.join(datum)
   print >> file, ' '.join(map(str, 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

(snip)

One of the nice things with Python is the interactive shell. Let's use it:
Python 2.4.1 (#1, Jul 23 2005, 00:37:37)
[GCC 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6)] on 
linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> a = map(float, range(0,3))
 >>> a
[0.0, 1.0, 2.0]
 >>> b = map(float, range(12, 15))
 >>> c = map(float, range(7, 10))
 >>> a, b, c
([0.0, 1.0, 2.0], [12.0, 13.0, 14.0], [7.0, 8.0, 9.0])
 >>> str(a)
'[0.0, 1.0, 2.0]'
 >>> list(str(a))
['[', '0', '.', '0', ',', ' ', '1', '.', '0', ',', ' ', '2', '.', '0', ']']
 >>> zip(str(a), str(b), str(c))
[('[', '[', '['), ('0', '1', '7'), ('.', '2', '.'), ('0', '.', '0'), 
(',', '0', ','), (' ', ',', ' '), ('1', ' ', '8'), ('.', '1', '.'), 
('0', '3', '0'), (',', '.', ','), (' ', '0', ' '), ('2', ',', '9'), 
('.', ' ', '.'), ('0', '1', '0'), (']', '4', ']')]
 >>> zip(a, b, c)
[(0.0, 12.0, 7.0), (1.0, 13.0, 8.0), (2.0, 14.0, 9.0)]
 >>>

HTH
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help: output arrays into file as column

2006-07-27 Thread bei
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: help: output arrays into file as column

2006-07-27 Thread Simon Forman
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

-- 
http://mail.python.org/mailman/listinfo/python-list


help: output arrays into file as column

2006-07-27 Thread bei
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

-- 
http://mail.python.org/mailman/listinfo/python-list