En Fri, 21 Nov 2008 14:13:23 -0200, Joe Strout <[EMAIL PROTECTED]> escribió:
Right -- so using split() gives you the wrong answer for two different
reasons. Try these:
>>> line = "1 x"
>>> a, b = line.split() # b == "x", which is correct
>>> line = "2 "
>>> a, b = line.split() # correct answer would be b == " "
ValueError: need more than 1 value to unpack
>>> line = "3 x and here is some extra stuff"
>>> a, b = line.split() # correct answer would be b == "x and here is
some extra stuff"
ValueError: too many values to unpack
Partition handles these cases correctly (at least, within the OP's
specification that the value of "b" should be whatever comes after the
first space).
split takes an additional argument too:
py> line = "3 x and here is some extra stuff"
py> a, b = line.split(None, 1)
py> a
'3'
py> b
'x and here is some extra stuff'
But it still fails if the line contains no spaces. partition is more
robust in those cases
--
Gabriel Genellina
--
http://mail.python.org/mailman/listinfo/python-list