On 20/06/11 20:14:46, Tim Johnson 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?

That was 2.6, according to the online docs.

Take a look at the documentation that came with your Python
installation.  The documentation for str.format ends with:
"New in version 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':""}
   ?

Opinions differ.  Some people would use the 're' module:

import re

S = 'Coordinates: {latitude}, {longitude}'

keys = re.findall(r'{(\w+)}', S)

print '{' + ', '.join("'" + k + '\':""' for k in keys) + '}'


Other people prefer to use string methods:

S = 'Coordinates: {latitude}, {longitude}'

start = -1
keys = []
while True:
    start = S.find('{', start+1)
    if start == -1:
        break
    end = S.find('}', start)
    if end > start:
        keys.append(S[start+1:end])

print '{' + ', '.join("'" + k + '\':""' for k in keys) + '}'


It might be a matter of taste; it might depend on how familiar
you are with 're'; it might depend on what you mean by 'optimal'.

-- HansM


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to