8 Replies - 2202 Views - Last Post: 09 January 2011 - 01:27 PM Rate Topic: -----

#1 tcl76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-January 11

Open file, Pattern Matching

Posted 08 January 2011 - 08:59 PM

hi,

i have log file as below. if found -1, how to list out all the Lane number? And if there is no -1, print PASS. tq
Platform: PC
Tempt : 25
TAP0 :0
TAP1 :1

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

####code
fname = "sampledata.txt" 
pattern = "-1"
for search in open(fname):
    if pattern in search:
        print "FAIL"
    else:
        print "PASS"

This post has been edited by atraub: 08 January 2011 - 09:34 PM
Reason for edit:: Inserted code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Open file, Pattern Matching

#2 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Open file, Pattern Matching

Posted 08 January 2011 - 09:33 PM

I have absolutely no idea what you're asking.
Was This Post Helpful? 0
  • +
  • -

#3 tcl76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-January 11

Re: Open file, Pattern Matching

Posted 08 January 2011 - 10:45 PM

View Postatraub, on 08 January 2011 - 08:33 PM, said:

I have absolutely no idea what you're asking.

what i meant is if i find -1, i want to print out which Lane contains the -1. For eg: in the sampledata. lane 2 and 4 contain the -1. i want to print them as Lane 2 Fail. Lane 4 Fail. If there are no -1 in the sampledata, i want to print out as All Lanes Pass.
sorry for the confusion. looking forward for advice. tq
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Open file, Pattern Matching

Posted 09 January 2011 - 06:25 AM

Here's how I would do it:

####code
fname = "sampledata.txt" 
pattern = "-1"
failure = False
for search in open(fname):
    if pattern in search:
        print 'Lane', search.split()[2], 'Fail'
if not failure:
    print "All Lanes Pass"


Was This Post Helpful? 0
  • +
  • -

#5 tcl76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-January 11

Re: Open file, Pattern Matching

Posted 09 January 2011 - 06:39 AM

View PostMotoma, on 09 January 2011 - 05:25 AM, said:

Here's how I would do it:

####code
fname = "sampledata.txt" 
pattern = "-1"
failure = False
for search in open(fname):
    if pattern in search:
        print 'Lane', search.split()[2], 'Fail'
if not failure:
    print "All Lanes Pass"




thanks for your advice. if my sampledata is as below:
Platform: PC
Tempt : 25
TAP0 :0
TAP1 :1

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

#####output ########
Lane 2 Fail
Lane 4 Fail
All Lanes Pass
####################

Question why does does it print out All Lanes Pass? appreciate your advice.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Open file, Pattern Matching

Posted 09 January 2011 - 06:52 AM

Because you never set the variable Failure to anything but what you initialized it as, which was False?
Was This Post Helpful? 0
  • +
  • -

#7 tcl76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-January 11

Re: Open file, Pattern Matching

Posted 09 January 2011 - 07:03 AM

could you pls elaborate. The variable failure is set to False initially. it works when there is no -1 in the sampledata, then it will print All Lanes Pass. But if there are more than one -1, then it will print the Lane number that fail and in addition to it also print out All Lanes Pass. How to avoid printing out All Lanes Pass. tq
Was This Post Helpful? 0
  • +
  • -

#8 tcl76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 14
  • Joined: 05-January 11

Re: Open file, Pattern Matching

Posted 09 January 2011 - 07:44 AM

finally modified the code to get it working. thanks for all the great advice, learning a lot from you all. :)

fname = "sampledata.txt" 
pattern = "-1"
failure = False
for search in open(fname):
    if pattern in search:
        #print search
        #print failure
        failure=True
        print 'Lane', search.split()[2], 'Fail'
        
if not failure:    
    print "All Lanes Pass"


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:
Was This Post Helpful? 1
  • +
  • -

#9 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Open file, Pattern Matching

Posted 09 January 2011 - 01:27 PM

Whoops, sorry about the confusion. I just whipped up that code in the text editor; I should have ran it through before I posted it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1