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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mech point

    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

  • John Machin

    #2
    Re: Read/write 2D data from/to file..?

    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'.j oin(' '.join(repr(ite m)for item in
    row)for row in rows)+'\n')


    Comment

    • Grant Edwards

      #3
      Re: Read/write 2D data from/to file..?

      On 2007-02-12, mech point <srikanth.allu@ 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

      Comment

      • Gabriel Genellina

        #4
        Re: Read/write 2D data from/to file..?

        En Sun, 11 Feb 2007 22:47:30 -0300, mech point <srikanth.allu@ 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.writel ines(",".join(m ap(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

        Comment

        • Grant Edwards

          #5
          Re: Read/write 2D data from/to file..?

          On 2007-02-12, Grant Edwards <grante@visi.co mwrote:
          On 2007-02-12, mech point <srikanth.allu@ 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?

          Comment

          • Vasily Sulatskov

            #6
            Re: Read/write 2D data from/to file..?

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



            Comment

            Working...