help: output arrays into file as column
Bruno Desthuilliers
bdesth.quelquechose at free.quelquepart.fr
Thu Jul 27 16:43:09 EDT 2006
bei a écrit :
<ot> Please don't top-post</ot>
> 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
More information about the Python-list
mailing list