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

Grant Edwards grante at visi.com
Sun Feb 11 22:12:44 EST 2007


On 2007-02-12, Grant Edwards <grante at visi.com> wrote:
> On 2007-02-12, mech point <srikanth.allu at gmail.com> 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?



More information about the Python-list mailing list