473,513 Members | 2,558 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to check value in a column?

30 New Member
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
5 2279
maximus tee
30 New Member
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
maximus tee
30 New Member
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
2,851 Recognized Expert Moderator Specialist
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
maximus tee
30 New Member
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
2,851 Recognized Expert Moderator Specialist
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

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

Similar topics

0
1808
by: Bajal Mohamed via SQLMonster.com | last post by:
I am facing problem like; We generate reports thru Excel Pivot table. The data is coming from sales cube. Let?s assume that there are 2 products A and B. The user wants to filter for each...
1
1236
by: Daylor | last post by:
hi. i want to display page. then check every 10 seconds from the client, if a value in the server is changed WITHOUT RELOADING THE ALL PAGE. if value has change, i want the client to reload...
1
4214
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
11
3020
by: Richard Meister | last post by:
Hi, I'd like to define several constants and make sure that all of them are smaller than a given other constant. I thought this could be done by a simple macro. Something like this: #define...
1
1942
by: Eric | last post by:
This is the query which i run when the form load. In tbl_EquipmentChronology there are three variables Equipment1, Equipment2, Equipment3. If Equipment1 and 2 are not empty then the query copy...
1
1447
by: Anirhudra | last post by:
I have a timer and this timer value I have to compare with time chunk about 5 second ... it would be like this Here timercount = what the time I have now .. means now I have 10 sec t = time...
4
2332
by: Torilyn73 | last post by:
I have a combo box set up off a query. I want the default value to be the column heading so I don't have to put a label over it. So far I haven't been able to figure this out. Can anyone tell me if...
0
981
by: Ganesh | last post by:
Hi There, I've a datagrid in my application, template column contains a check box plus more columns from my table. when user clicks checkbox i call javascript to make sure at any time one...
2
1805
by: pashki | last post by:
I have create trigger: CREATE TRIGGER TR NO CASCADE BEFORE INSERT ON T REFERENCING NEW AS newrow FOR EACH ROW MODE DB2SQL WHEN (newrow.id = null) BEGIN ATOMIC set newrow.id = nextval for...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.