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


Groups > comp.lang.python > #8025 > unrolled thread

Re: Parsing a dictionary from a format string

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2011-06-20 13:34 -0600
Last post2011-06-20 13:34 -0600
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.


Contents

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

#8025 — Re: Parsing a dictionary from a format string

FromIan Kelly <ian.g.kelly@gmail.com>
Date2011-06-20 13:34 -0600
SubjectRe: Parsing a dictionary from a format string
Message-ID<mailman.189.1308598518.1164.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web