Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #94337 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2015-07-22 11:11 +1000 |
| Last post | 2015-07-22 11:11 +1000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: convert output to list(and nested dictionary) Chris Angelico <rosuav@gmail.com> - 2015-07-22 11:11 +1000
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2015-07-22 11:11 +1000 |
| Subject | Re: convert output to list(and nested dictionary) |
| Message-ID | <mailman.846.1437527515.3674.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web