converting from perl: variable sized unpack

Barry A. Warsaw barry at
Fri Jul 13 23:28:39 EDT 2001

>>>>> "RS" == Roy Smith <roy at> writes:

    RS> What's the best way to translate this perl snippet into
    RS> python:

    RS>  ($f1, $f2, $f3, $f4, $f5, $f6) = split ('\s+', $line);

    RS> The obvious translation would be:

    RS>  [f1, f2, f3, f4, f5, f6] = string.split (line)

    RS> but the problem is that line may have fewer than 6 fields on
    RS> it.  Perl handles this smoothly by leaving the "extra"
    RS> variables undefined, whereas python raises a ValueError
    RS> exception.  What I'd like to do is have the extra field
    RS> variables left as null strings.

Python doesn't work this way, but I've been thinking about writing a
PEP to make sequence unpacking less strict.


More information about the Python-list mailing list