convert output to list(and nested dictionary)

Chris Angelico rosuav at gmail.com
Wed Jul 22 03:11:53 CEST 2015


On Wed, Jul 22, 2015 at 11:03 AM, Pablo Lucena <plucena24 at 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


More information about the Python-list mailing list