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

John Machin sjmachin at lexicon.net
Sun Feb 11 21:24:18 EST 2007


On Feb 12, 12:47 pm, "mech point" <srikanth.a... 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.

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')





More information about the Python-list mailing list