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)

Path csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!bcyclone04.am1.xlned.com!bcyclone04.am1.xlned.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'cc:addr:python-list': 0.09; '22,': 0.09; 'loop.': 0.09; 'underlying': 0.09; 'python': 0.10; 'output': 0.13; 'def': 0.13; 'wed,': 0.15; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'textual': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'parse': 0.22; 'am,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'subject:list': 0.26; 'message-id:@mail.gmail.com': 0.27; 'objects': 0.29; 'received:google.com': 0.35; 'subject:: ': 0.37; 'skip:p 20': 0.38; 'jul': 0.72; 'chrisa': 0.84; 'pablo': 0.84; 'to:none': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=uLeGgqG5iu2ZZwEdh9ZD7yHuxIB2cKoFw8AGdpNO9hY=; b=UpRDNecI4Qp5Ar8Bqd69Ls7HQXct1O97dDGIk2FpxwJGWty9tvJNM+BN89RN6al7Bw 56orMUI27l4z3HlgiCJ2BtyZ1lCE9wMkddGt9a0vQz7IZkYOUnMO3Or08786lZB7dHPi tRxNC4JHfYg0p1NvslsjnQwU2tY6WNpno2/iBfXeoiNDgH1fTLhvKy5VaDcdjpOq0TBC wxaF4rW0yVIC4VXcgd3XGC1oC71Dq3avUmzzHnESO+FGPCczEEB7I5um7VAP8Xe7GAxj w2fl/+jTrv7qO1N6dwWJJ9K+/zwGdDIwm0B6sErHkt4XTYal5cQlfAHS9RMT/B9axkLN RgaQ==
MIME-Version 1.0
X-Received by 10.107.132.7 with SMTP id g7mr50825360iod.9.1437527513733; Tue, 21 Jul 2015 18:11:53 -0700 (PDT)
In-Reply-To <CAB_tDZyB+mCg9yWg9oG0-OL-NHVfCSifkMrD0x3k4LPNx-dhNw@mail.gmail.com>
References <CAKoJ+qB7mLX+tLAqsO=nSocjVLtLn+TCWZ34z8YVFCKKt10ntQ@mail.gmail.com> <CAB_tDZyB+mCg9yWg9oG0-OL-NHVfCSifkMrD0x3k4LPNx-dhNw@mail.gmail.com>
Date Wed, 22 Jul 2015 11:11:53 +1000
Subject Re: convert output to list(and nested dictionary)
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.846.1437527515.3674.python-list@python.org> (permalink)
Lines 19
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1437527515 news.xs4all.nl 2833 [2001:888:2000:d::a6]:44346
X-Complaints-To abuse@xs4all.nl
X-Received-Bytes 3651
X-Received-Body-CRC 553828282
Xref csiph.com comp.lang.python:94337

Show key headers only | 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