reading a column from a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Wessle

    #1

    reading a column from a file

    Hi

    I have a file with data like
    location pressure temp
    str floot floot

    I need to read pressure and temp in 2 different variables so that I
    can plot them as lines. is there a package which reads from file with
    a given formate and returns desired variables? or I need to open,
    while not EOF read, parse, build list, return?

    thanks
  • pyGuy

    #2
    Re: reading a column from a file

    f = open("datafile. txt", "r")
    data = [line.split('\t' ) for line in f]
    f.close()
    pressure = [float(d[1]) for d in data]
    temp = [float(d[2]) for d in data]
    ---------------------------------------------------

    This will parse the file into a matrix stored in 'data'. The last two
    lines simply iterate through second and third columns respectively,
    converting each element to a float (from string as it was read in from
    file) and assign to the appropriate vars.

    Comment

    • Larry Bates

      #3
      Re: reading a column from a file

      Check out the csv module.

      -Larry Bates

      Gary Wessle wrote:[color=blue]
      > Hi
      >
      > I have a file with data like
      > location pressure temp
      > str floot floot
      >
      > I need to read pressure and temp in 2 different variables so that I
      > can plot them as lines. is there a package which reads from file with
      > a given formate and returns desired variables? or I need to open,
      > while not EOF read, parse, build list, return?
      >
      > thanks[/color]

      Comment

      • Larry Bates

        #4
        Re: reading a column from a file

        Check out the csv module.

        -Larry Bates

        Gary Wessle wrote:[color=blue]
        > Hi
        >
        > I have a file with data like
        > location pressure temp
        > str floot floot
        >
        > I need to read pressure and temp in 2 different variables so that I
        > can plot them as lines. is there a package which reads from file with
        > a given formate and returns desired variables? or I need to open,
        > while not EOF read, parse, build list, return?
        >
        > thanks[/color]

        Comment

        Working...