converting from perl: variable sized unpack

Roman Suzi rnd at onego.ru
Sat Jul 14 02:26:50 EDT 2001


On Fri, 13 Jul 2001, James Logajan wrote:

>Roy Smith wrote:
>>
>> What's the best way to translate this perl snippet into python:
>>
>>  ($f1, $f2, $f3, $f4, $f5, $f6) = split ('\s+', $line);
>>
>> The obvious translation would be:
>>
>>  [f1, f2, f3, f4, f5, f6] = string.split (line)
>>
>> but the problem is that line may have fewer than 6 fields on it.  Perl
>> handles this smoothly by leaving the "extra" variables undefined, whereas
>> python raises a ValueError exception.  What I'd like to do is have the
>> extra field variables left as null strings.
>
>This may not be the most elegant or efficient, but how about this:
>
>[f1, f2, f3, f4, f5, f6] = (string.split (line) + 6*[None])[:6]

My variant is:

f1, f2, f3, f4, f5, f6 = map(lambda x, y: x, string.split(line), 6*[None])

(maybe 6*[None] could be written as xrange(6), but I am not sure if
xrange will allow it after stripping down)

- this is VERY common problem when analysing logs. I will be grateful
if somebody could point me to how to give names to log fields
in efficient manner (logs are long, you know!). Probably, this
is better done in C.

Maybe, some kind of support from string module or Python itself
could be had, for example:

f1, f2, f3, f4, f5, f6 = pad(string.split(line), 6, None)

(where None could be omitted to be default value)

There could also be rpad to pad from right.

*

re module could also be used, with named groups which
name fields. But I wonder how efficient it is to use re
where split is enough...


Sincerely yours, Roman Suzi
-- 
_/ Russia _/ Karelia _/ Petrozavodsk _/ rnd at onego.ru _/
_/ Saturday, July 14, 2001 _/ Powered by Linux RedHat 6.2 _/
_/ "A mainframe: The biggest PC peripheral available." _/





More information about the Python-list mailing list