469,338 Members | 8,284 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

Read/write 2D data from/to file..?


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.

Thank you,
srikanth

Feb 12 '07 #1
5 7540
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'.join(' '.join(repr(item)for item in
row)for row in rows)+'\n')
Feb 12 '07 #2
On 2007-02-12, mech point <sr***********@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
Feb 12 '07 #3
En Sun, 11 Feb 2007 22:47:30 -0300, mech point <sr***********@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.writelines(",".join(map(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]))

--
Gabriel Genellina

Feb 12 '07 #4
On 2007-02-12, Grant Edwards <gr****@visi.comwrote:
On 2007-02-12, mech point <sr***********@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?
Feb 12 '07 #5
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.
Using matplotlib it will be:

import pylab
rows = pylab.load('src.dat')
pylab.save(rows, 'dst.dat')

Feb 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by jas | last post: by
1 post views Thread by Magix | last post: by
3 posts views Thread by nicolasg | last post: by
8 posts views Thread by dosworldguy | last post: by
2 posts views Thread by Kevin Ar18 | last post: by
9 posts views Thread by vineeth | last post: by
5 posts views Thread by dm3281 | last post: by
1 post views Thread by Sachin Garg | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.