hi all,
thanks for all your advice, i really learn a lot.
finally i modified the code to be working as:
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"
thanks
tcl76
> To: [email protected]
> From: [email protected]
> Date: Sun, 9 Jan 2011 14:14:17 +0000
> Subject: Re: [Tutor] Open a text file, read and print pattern matching
>
>
> "tee chwee liong" <[email protected]> wrote
>
> > File "C:/Python25/myscript/log/readfile9.py", line 5, in <module>
> > port, channel, lane, eyvt = line.split()
> > ValueError: need more than 2 values to unpack
>
> So you need to ensure that you unpack when you have the right
> number of fields:
>
> either:
>
> data = line.split()
> if len(data) = 4:
> port, channel, lane, eyvt = data
> else: continue
> # rest of your code here...
>
>
> or more pythonically:
>
> try:
> port, channel, lane, eyvt = line.split()
> # the rest of your code here
> except ValueError: continue
>
> HTH,
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
> _______________________________________________
> Tutor maillist - [email protected]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor