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


Groups > comp.lang.python > #8025

Re: Parsing a dictionary from a format string

References <20110620181446.GI1971@johnsons-web.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2011-06-20 13:34 -0600
Subject Re: Parsing a dictionary from a format string
Newsgroups comp.lang.python
Message-ID <mailman.189.1308598518.1164.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Jun 20, 2011 at 12:14 PM, Tim Johnson <tim@johnsons-web.com> wrote:
> Currently using python 2.6, but am serving some systems that have
> older versions of python (no earlier than.
> Question 1:
>  With what version of python was str.format() first implemented?

2.6

> Question 2:
>  Given the following string:
>    S = 'Coordinates: {latitude}, {longitude}'
>  Is there a python library that would provide an optimal way
>    to parse from S the following
>  {'latitude':"",'longitude':""}
>  ?

import re
match = re.match('^Coordinates: (.+), (.+)$', S)
if match:
    data = {'latitude': match.group(1), 'longitude': match.group(2)}

Or you could use string methods and slicing to extract the figures,
which would be faster than the regular expression machinery.

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


Thread

Re: Parsing a dictionary from a format string Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-20 13:34 -0600

csiph-web