Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94337

Re: convert output to list(and nested dictionary)

References <CAKoJ+qB7mLX+tLAqsO=nSocjVLtLn+TCWZ34z8YVFCKKt10ntQ@mail.gmail.com> <CAB_tDZyB+mCg9yWg9oG0-OL-NHVfCSifkMrD0x3k4LPNx-dhNw@mail.gmail.com>
Date 2015-07-22 11:11 +1000
Subject Re: convert output to list(and nested dictionary)
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.846.1437527515.3674.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Jul 22, 2015 at 11:03 AM, Pablo Lucena <plucena24@gmail.com> wrote:
> str.split and re are a nice quick way to do it:
>
>>>> def get_data(data):
> import re
> port_re = re.compile(r'(\w+)\((\S+-\S+)\)')
> cidr_re = re.compile(r'\[(.*?)\]')
> _, proto_port, cidr = data.rsplit(":", 2)
> port_match = port_re.search(proto_port)
> proto, port = port_match.group(1), port_match.group(2)
> port = port.split("-")[0]
> cidr_match = cidr_re.search(cidr)
> cidr = cidr_match.group(1)
> return dict(port=port, proto=proto, cidr=cidr)

The textual output is coming from his quick little Python loop. No
need to parse that when you can go to the underlying objects :)

ChrisA

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: convert output to list(and nested dictionary) Chris Angelico <rosuav@gmail.com> - 2015-07-22 11:11 +1000

csiph-web