473,671 Members | 2,326 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 2286
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
1827
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 product and view ?stock in hand? and ?total sales? for particular month (as two columns). From the Pivot table filtering, when we choose product A and month ?Jan?, it is showing values for ?stock in hand? and ?total sales? columns for that month.
1
1245
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 the page again.
1
4229
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 the example to work. I created the following two classes, provided with the example: *-*-**-*-*-*-*-*-*-*-*-*-**-*-*-*-*-CheckBoxColumn Class:-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-**-*-*-*
11
3038
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 MAX 999 #define DEF_CHECKED_VAL( name, value) #if (value < MAX) \ #define name MAX \
1
1952
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 equipment2 value in txt of table tbl_Event. Is it possible i do the same IIF or any other way i check into DLookup. I mean same if in tbl_EquipmentChronology Equipment1, 2 and 3 are not empty it shows the value from Equipment3 in List17. Thanks
1
1457
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 Chunk 5 if (timercount<=t) sleep 1000 ms always check current timer value with t else
4
2340
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 this is possible... and if so..how? Thanks!
0
987
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 checkbox is clicked. it works fine. Now i'd like to check another bound column datetime, when user click checkbox i want to check whether the date is in current month and year, if not just a alert to user to tell you cannot select this because not in...
2
1812
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 SAMPLE.sequ ;
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8909
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8596
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5690
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4221
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4399
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2806
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 we have to send another system
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.