By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,035 Members | 1,408 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,035 IT Pros & Developers. It's quick & easy.

How to check value in a column?

P: 30
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====================== =======
Expand|Select|Wrap|Line Numbers
  1. print 
  2. fname = raw_input('Enter filename: ') 
  3. pattern = raw_input('Enter pattern: ') 
  4.  
  5. def findPattern(fname, pat): 
  6.  
  7.     f = open(fname, "r") 
  8.     for line in f: 
  9.         if pat in line: 
  10.             print "Fail"
  11.             break
  12.  
  13.     else:
  14.  
  15.         print "Pass"     
  16.  
  17. 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
Jan 8 '11 #1
Share this Question
Share on Google+
5 Replies


P: 30
i edited my code to but i think there must be better way to do it. any advice.
thanks
maximus
Expand|Select|Wrap|Line Numbers
  1. f = open(filepath,'r')
  2. line_num=0
  3. search_phrase = "-1" 
  4. for line in f.readlines(): 
  5.     line_num += 1   
  6.  
  7.     if line.find(search_phrase) >= 0:        
  8.         print "Fail:Lane %s" %(line_num-8)        
  9.         break
Jan 8 '11 #2

P: 30
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.
Jan 9 '11 #3

bvdet
Expert Mod 2.5K+
P: 2,851
I would parse the file, then look for a failure. Here's a stab at it:
Expand|Select|Wrap|Line Numbers
  1. def parse(fn):
  2.     f = open(fn)
  3.  
  4.     # look for two lines starting with "+"
  5.     # the data will start on the next line
  6.     cnt = 0
  7.     indata = False
  8.  
  9.     # save data in a dictionary
  10.     dd = {}
  11.  
  12.     for line in f:
  13.         if line.startswith("+"):
  14.             if cnt == 1:
  15.                 lineNo = 0
  16.                 while True:
  17.                     lineNo += 1
  18.                     line = f.next().strip()
  19.                     if line.startswith("+"):
  20.                         f.close()
  21.                         return dd
  22.                     dd[lineNo] = line.split()
  23.             else:
  24.                 cnt += 1
  25.  
  26.  
  27. fn = 'data10.txt'
  28. dd = parse(fn)
  29. for key in dd:
  30.     if "-1" in dd[key]:
  31.         print "Line number %s failed at column %s" % (key, dd[key].index("-1")+1)
Jan 9 '11 #4

P: 30
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:
Expand|Select|Wrap|Line Numbers
  1.    if "-1" in dd[key]: 
  2.         print "Line number %s failed at column %s" % (key, dd[key].index("-1")+1)
  3.     else:
  4.         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
Jan 9 '11 #5

bvdet
Expert Mod 2.5K+
P: 2,851
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 "+".
Jan 9 '11 #6

Post your reply

Sign in to post your reply or Sign up for a free account.