converting from perl: variable sized unpack

Eric Hagemann ehagemann at home.com
Sat Jul 14 18:24:01 EDT 2001


I have a lot of code to parse logs as well --  neat trick (at least I think
it neat !)
to overcome this "problem"  is

name_of_field0 = 0
name_of_field1 = 1
name_of_field2 = 2
.
f = line.split()
.

then access the elements as
foo = f[name_of_field1]

in my applications I can use the len(f) to determine the type of line I am
parsing (by its length)

This way you get variable length parsing as well as "named" arguments

Cheers


"Roman Suzi" <rnd at onego.ru> wrote in message
news:mailman.995092164.12281.python-list at python.org...
> 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