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

Discussion in 'Python' started by mech point, Feb 12, 2007.

  1. mech point

    mech point Guest

    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
    mech point, Feb 12, 2007
    #1
    1. Advertising

  2. mech point

    John Machin Guest

    On Feb 12, 12:47 pm, "mech point" <> 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')
    John Machin, Feb 12, 2007
    #2
    1. Advertising

  3. On 2007-02-12, mech point <> 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
    Grant Edwards, Feb 12, 2007
    #3
  4. En Sun, 11 Feb 2007 22:47:30 -0300, mech point <>
    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
    Gabriel Genellina, Feb 12, 2007
    #4
  5. On 2007-02-12, Grant Edwards <> wrote:
    > On 2007-02-12, mech point <> 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?
    Grant Edwards, Feb 12, 2007
    #5
  6. On Feb 12, 6:47 am, "mech point" <> 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')
    Vasily Sulatskov, Feb 12, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sweety
    Replies:
    9
    Views:
    1,010
    Richard Heathfield
    Feb 7, 2006
  2. Ram
    Replies:
    3
    Views:
    245
    Tad McClellan
    Apr 26, 2007
  3. JL
    Replies:
    2
    Views:
    87
    Tim Chase
    Dec 14, 2013
  4. Cameron Simpson
    Replies:
    0
    Views:
    83
    Cameron Simpson
    Dec 15, 2013
  5. Tim Chase
    Replies:
    0
    Views:
    86
    Tim Chase
    Dec 16, 2013
Loading...

Share This Page