On Feb 12, 12:47 pm, "mech point" <srikanth.a...@ gmail.comwrote:
I was able to read the data from file into a two dimensional array
(lists)
>
rows=[map(float,line. split())for line in file("data")]
>
but How to write them back into the file.
Presuming that it is either mandatory to adopt the same style (or lack
thereof) as the input code, and/or futile to suggest otherwise:
file('data2','w ').write('\n'.j oin(' '.join(repr(ite m)for item in
row)for row in rows)+'\n')
On 2007-02-12, mech point <srikanth.allu@ gmail.comwrote:
>
I was able to read the data from file into a two dimensional array
(lists)
>
rows=[map(float,line. split())for line in file("data")]
>
but How to write them back into the file.
for r in rows:
file.write(" ".join(map(str, r)) + "\n")
--
Grant Edwards grante Yow! My nose feels like a
at bad Ronald Reagan movie...
visi.com
En Sun, 11 Feb 2007 22:47:30 -0300, mech point <srikanth.allu@ gmail.com>
escribió:
I was able to read the data from file into a two dimensional array
(lists)
>
rows=[map(float,line. split())for line in file("data")]
>
but How to write them back into the file.
This way uses the same structures as your example; line.split(",") ->
",".join(.. .); map(float,...) -map(str,...)
yourfile.writel ines(",".join(m ap(str,row))+"\ n" for row in rows)
If you are using Python<2.5, put [] inside the writelines call:
writelines([","...]).
Or move the iteration outer. If you want control on the format too:
for row in rows:
yourfile.write( "%.2f,%.6g\ n" % (row[0], row[1]))
On 2007-02-12, Grant Edwards <grante@visi.co mwrote:
On 2007-02-12, mech point <srikanth.allu@ gmail.comwrote:
>>
>I was able to read the data from file into a two dimensional array
>(lists)
>>
>rows=[map(float,line. split())for line in file("data")]
>>
>but How to write them back into the file.
>
for r in rows:
file.write(" ".join(map(str, r)) + "\n")
Doh. Bad choice of names for my file object:
f = file("data","w" )
for r in rows:
f.write(" ".join(map(str, r)) + "\n")
You can do it on one line if you want, but I find the above a
little bit clearer.
--
Grant Edwards grante Yow! Spreading peanut
at butter reminds me of
visi.com opera!! I wonder why?
On Feb 12, 6:47 am, "mech point" <srikanth.a...@ gmail.comwrote:
I was able to read the data from file into a two dimensional array
(lists)
>
rows=[map(float,line. split())for line in file("data")]
>
but How to write them back into the file.
Comment