How to check value in a column?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maximus tee
    New Member
    • Dec 2010
    • 30

    How to check value in a column?

    hi,

    i have a text file(as below). how to check if there is -1, print out which lane is it and fail it. Eg: FAIL Lane 4. My code is like this, but only check whether there is -1 in the file.
    =============== =========Code== =============== ============
    Code:
    print 
    fname = raw_input('Enter filename: ') 
    pattern = raw_input('Enter pattern: ') 
     
    def findPattern(fname, pat): 
     
        f = open(fname, "r") 
        for line in f: 
            if pat in line: 
                print "Fail"
                break
                
        else:
    
            print "Pass"     
     
    findPattern(fname, pattern)
    =============== =====Data====== =============== ============
    Platform: PC
    Tempt : 25
    TAP0 :0
    TAP1 :1

    +++++++++++++++ +++++++++++++++ +++++++++++++++
    Port Chnl Lane EyVt
    +++++++++++++++ +++++++++++++++ +++++++++++++++
    0 1 1 75
    0 1 2 55
    0 1 3 65
    0 1 4 -1
    0 1 5 20
    +++++++++++++++ +++++++++++++++ +++++++++++++++
    Time: 20s

    thanks
    maximus
    Last edited by bvdet; Jan 9 '11, 02:55 AM. Reason: Add code tags
  • maximus tee
    New Member
    • Dec 2010
    • 30

    #2
    i edited my code to but i think there must be better way to do it. any advice.
    thanks
    maximus
    Code:
    f = open(filepath,'r')
    line_num=0
    search_phrase = "-1" 
    for line in f.readlines(): 
        line_num += 1   
        
        if line.find(search_phrase) >= 0:        
            print "Fail:Lane %s" %(line_num-8)        
            break
    Last edited by bvdet; Jan 9 '11, 02:56 AM. Reason: Add code tags

    Comment

    • maximus tee
      New Member
      • Dec 2010
      • 30

      #3
      the code i had written so far didn't work in cases like when there are more than one Lane having -1. any advice on how to edit the code so that it will display all lanes fail when there is -1 value. thanks.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        I would parse the file, then look for a failure. Here's a stab at it:
        Code:
        def parse(fn):
            f = open(fn)
            
            # look for two lines starting with "+"
            # the data will start on the next line
            cnt = 0
            indata = False
        
            # save data in a dictionary
            dd = {}
        
            for line in f:
                if line.startswith("+"):
                    if cnt == 1:
                        lineNo = 0
                        while True:
                            lineNo += 1
                            line = f.next().strip()
                            if line.startswith("+"):
                                f.close()
                                return dd
                            dd[lineNo] = line.split()
                    else:
                        cnt += 1
        
        
        fn = 'data10.txt'
        dd = parse(fn)
        for key in dd:
            if "-1" in dd[key]:
                print "Line number %s failed at column %s" % (key, dd[key].index("-1")+1)

        Comment

        • maximus tee
          New Member
          • Dec 2010
          • 30

          #5
          thanks for your advice. can you pls elaborate a bit on the purpose of cnt and how the for loop works?
          i also added and else statement:
          Code:
             if "-1" in dd[key]: 
                  print "Line number %s failed at column %s" % (key, dd[key].index("-1")+1)
              else:
                  print "All lane PASS"
          However, this code reads each line and if it can't find -1, it'll print All lane PASS. pls advise how to print out All Lane PASS once only after checking there is no -1 the data file.

          thanks
          maximus
          Last edited by bvdet; Jan 9 '11, 02:47 PM. Reason: Add code tags

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            maximus,

            Please use code tags when posting code. It makes code readable and saves me from having to add them for you.

            The purpose of cnt is to keep up with the number of lines starting with "+".

            When the second line starting with "+" is found, we enter a for loop and iterate on the file object using file object method next() until we reach another line starting with "+".

            Comment

            Working...