[Tutor] Open a text file, read and print pattern matching

tee chwee liong tcl76 at hotmail.com
Sun Jan 9 15:12:44 CET 2011


hi,
 
thanks for the explanation. if i print out line=line.split(), below is the result: 
['Platform:', 'PC']
['Tempt', ':', '25']
['TAP0', ':0']
['TAP1', ':1']
[]
['+++++++++++++++++++++++++++++++++++++++++++++']
['Port', 'Chnl', 'Lane', 'EyVt']
['+++++++++++++++++++++++++++++++++++++++++++++']
['0', '1', '1', '75']
['0', '1', '2', '10']
['0', '1', '3', '-1']
['0', '1', '4', '-1']
['0', '1', '5', '10']
['+++++++++++++++++++++++++++++++++++++++++++++']
['Time:', '20s']

that's why port, channel, lane, eyvt = line.split() will give traceback error due to there are only 2 values in the 1st line. how to make the line search at line Port, Chnl, Lane, EyVt?
 
thanks
tcl76

 
> Date: Mon, 10 Jan 2011 00:48:51 +1100
> From: steve at pearwood.info
> To: tutor at python.org
> Subject: Re: [Tutor] Open a text file, read and print pattern matching
> 
> tee chwee liong wrote:
> > hi,
> > 
> > there is error when running the code:
> >> Traceback (most recent call last):
> >> 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
> > 
> > the error is due to below line code:
> >> port, channel, lane, eyvt = line.split()
> > 
> > pls advise. 
> 
> 
> You have a line with only 2 words, and you are trying to unpack it into 
> four variables. Four variables on the left, 4 values on the right works:
> 
> >>> a, b, c, d = "sentence with four words".split()
> >>> print a
> sentence
> >>> print b
> with
> >>> print c
> four
> >>> print d
> words
> 
> But 4 variables on the left, 3 on the right does not:
> 
> >>> a, b, c, d = "sentence with three".split()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: need more than 3 values to unpack
> 
> Likewise:
> 
> >>> a, b, c, d = "two words".split()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: need more than 2 values to unpack
> 
> >>> a, b, c, d = "word".split()
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: need more than 1 value to unpack
> 
> 
> 
> 
> -- 
> Steven
> 
> 
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110109/fddccdc6/attachment.html>


More information about the Tutor mailing list