converting from perl: variable sized unpack

Barry A. Warsaw barry at digicool.com
Sat Jul 14 05:28:39 CEST 2001


>>>>> "RS" == Roy Smith <roy at panix.com> 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.

-Barry




More information about the Python-list mailing list