need help in output table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • runsun
    New Member
    • May 2007
    • 17

    need help in output table

    want to read selected lines in a file, then output in one row.
    For example:
    read the 1st, 3rd ... lines, then output them in the 1st row;
    read the 2nd, 4th ... lines, output in the 2nd row;
    ...

    I used several loops. None of them worked.

    Thanks.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by runsun
    want to read selected lines in a file, then output in one row.
    For example:
    read the 1st, 3rd ... lines, then output them in the 1st row;
    read the 2nd, 4th ... lines, output in the 2nd row;
    ...

    I used several loops. None of them worked.

    Thanks.
    Use readlines() and then select the lines you want to print out using list comprehension:
    [code=python]
    myfile = open("test.txt" , "r")
    mylines = myfile.readline s()
    print [line for line in mylines if mylines.index(l ine)%2 == 0]
    print [line for line in mylines if mylines.index(l ine)%2 != 0]
    [/code]
    I think that should work, but I haven't tried it.

    Comment

    • runsun
      New Member
      • May 2007
      • 17

      #3
      Among the solutions, this looks like the simplest and most efficient one.
      The only problem is that the output includes some symbols. For example:
      the 1st row is
      ['01\n', '03\n', '05\n']

      Since I deal with numbers, I first convert them to int or float, then the output is what I want now:
      01 03 05

      Although I still don't know how it works if them are strings.
      Thanks a lot!!!


      Originally posted by ilikepython
      Use readlines() and then select the lines you want to print out using list comprehension:
      [code=python]
      myfile = open("test.txt" , "r")
      mylines = myfile.readline s()
      print [line for line in mylines if mylines.index(l ine)%2 == 0]
      print [line for line in mylines if mylines.index(l ine)%2 != 0]
      [/code]
      I think that should work, but I haven't tried it.

      Comment

      • ghostdog74
        Recognized Expert Contributor
        • Apr 2006
        • 511

        #4
        you just want to gather all odds and evens lines together:
        Code:
        odd=[];even=[]
        for num,line in enumerate(open("file")):
            num+=1
            if num%2==0: even.append(line.strip())
            elif num%2==1: odd.append(line.strip())
        print odd
        print even

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by runsun
          Among the solutions, this looks like the simplest and most efficient one.
          The only problem is that the output includes some symbols. For example:
          the 1st row is
          ['01\n', '03\n', '05\n']

          Since I deal with numbers, I first convert them to int or float, then the output is what I want now:
          01 03 05

          Although I still don't know how it works if them are strings.
          Thanks a lot!!!
          One of the first things that you'll need to understand is the difference between binary data and text data (each use different kinds of files). It is very common to save data in text form so that it may be read by people or other programs. Reading data saved as text, you must have you program convert the read info into workable data (in your case, floating point numbers). One cool thing about python is the print statement will take just about anything and convert it back to text for display. I'll give an example in the next post...

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by runsun
            Among the solutions, this looks like the simplest and most efficient one.
            The only problem is that the output includes some symbols. For example:
            the 1st row is
            ['01\n', '03\n', '05\n']

            Since I deal with numbers, I first convert them to int or float, then the output is what I want now:
            01 03 05

            Although I still don't know how it works if them are strings.
            Thanks a lot!!!
            I like what ilikepython has done here by reading all the data at once, then working on it. But instead of printing it, we'll convert it, then print the result to see that the conversion worked.
            To combine what my friend ilikepython has done with the idea of "parsing" data from a text file:
            [CODE=python]
            nRow = 2 # every other line from the file

            myfile = open("test.txt" , "r")
            mylines = myfile.readline s()
            myfile.close()

            nLines = len(mylines)
            nColumns = nLines/nRows
            table = [[] for i in range(nRows)] # this is an empty list of empty lists ("array")

            # Columnate a linear set of data #
            for i range(nRows):
            for j in range(0, nColumns, nRows): # start, stop, step
            table[i][j] = float(mylines[i + j].strip()) #Grab the data and convert stripped text all at once
            for row in table:
            for element in row:
            print element, # print all the elements of a row on one line using the ','
            [/CODE]

            This is not complete because it doesn't handle errors (like what if your data is not rectangular), etc. but should get you going in the right direction.

            Comment

            Working...