Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Read/write 2D data from/to file..?

Reply
Thread Tools

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

 
 
mech point
Guest
Posts: n/a
 
      02-12-2007

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

 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      02-12-2007
On Feb 12, 12:47 pm, "mech point" <(E-Mail Removed)> wrote:
> 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')


 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      02-12-2007
On 2007-02-12, mech point <(E-Mail Removed)> wrote:
>
> 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
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-12-2007
En Sun, 11 Feb 2007 22:47:30 -0300, mech point <(E-Mail Removed)>
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

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      02-12-2007
On 2007-02-12, Grant Edwards <(E-Mail Removed)> wrote:
> On 2007-02-12, mech point <(E-Mail Removed)> wrote:
>>
>> 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?
 
Reply With Quote
 
Vasily Sulatskov
Guest
Posts: n/a
 
      02-12-2007
On Feb 12, 6:47 am, "mech point" <(E-Mail Removed)> wrote:
> 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')



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
simple programs to deal with data format, data synchronisation Ram VHDL 1 02-24-2005 05:34 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi Cisco 0 12-29-2004 05:03 PM
Data Recovery Book and Online Business Promotion, Products Sales Promotion, Search Engine Optimization and Online Data Recovery Training services Author Tarun Tyagi Cisco 0 12-15-2004 01:52 AM
Data type question for OLEdb data types Brian Henry ASP .Net 1 10-05-2003 03:18 PM
Bind different rows to different data sources in data grid AD ASP .Net 1 08-23-2003 10:34 PM



Advertisments