FAQ
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?
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':""}
?
Thanks
--
Tim
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com

Search Discussions

  • Ian Kelly at Jun 20, 2011 at 7:34 pm

    On Mon, Jun 20, 2011 at 12:14 PM, 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? 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.
  • Tim Johnson at Jun 20, 2011 at 7:41 pm

    * Tim Johnson [110620 10:28]:
    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?
    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':""}
    ?
    P.S. :
    One solution would be
    import re
    re.findall(r'\{([^}]*)\}', s) ## I think, being a regex dummy..
    But perhaps there is something more efficient than re?
    cheers
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
  • Hans Mulder at Jun 20, 2011 at 8:08 pm

    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
  • Tim Johnson at Jun 20, 2011 at 8:49 pm

    * Hans Mulder [110620 12:15]:
    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?
    Duh!
    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'.
    As in speed.
    ## and then there is this - which I haven't tested a lot:
    def grabBetween(src,begin,end):
    """Grabs sections of text between `begin' and `end' and returns a list of
    0 or more sections of text."""
    parts = src.split(begin)
    res = []
    for part in parts:
    L = part.split(end)
    if len(L) > 1:
    res.append(L[0])
    return res

    I think later today, I will run some time tests using the `re'
    module as well as your function and the one above.

    BTW: To be more clear (hopefully) I was checking to see if there was
    a compiled method/function to do this.

    thanks
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com
  • Tim Johnson at Jun 20, 2011 at 10:39 pm
    * Tim Johnson [110620 13:00]:
    I think later today, I will run some time tests using the `re'
    module as well as your function and the one above.
    OK: Functions follow:
    def grabBetween(src,begin,end):
    """Grabs sections of text between `begin' and `end' and returns a list of
    0 or more sections of text."""
    parts = src.split(begin)
    res = []
    for part in parts:
    L = part.split(end)
    if len(L) > 1:
    res.append(L[0])
    return res
    def splitExtractDict(src,default):
    """Extract dictionary keys for a format string using
    `grabBetween', which uses the `split' string method."""
    D = {}
    keys = grabBetween(src,'{','}')
    for k in keys :
    D[k] = default
    return D
    def reExtractDict(src,default):
    """Extract dictionary keys for a format string using `re'"""
    D = {}
    keys = re.findall(r'\{([^}]*)\}', src)
    for k in keys :
    D[k] = default
    return D
    ## From Hans Mulder
    def findExtractDict(src,default):
    start = -1
    keys,D = [],{}
    while True:
    start = src.find('{', start+1)
    if start == -1:
    break
    end = src.find('}', start)
    if end > start:
    keys.append(src[start+1:end])
    for k in keys :
    D[k] = default
    return D
    ###################################################
    Now here are results using a small file and a lot of
    reps for each function call, just to give some meaningful
    times.
    ###################################################
    Using `split' : 0.0309112071991
    Using `re.find' : 0.0205819606781
    Using `find' : 0.0296318531036
    I will note that the last method did not produce
    correct results, but I also note that Hans did not
    promise tested code :).
    It is reasonable to suppose the `re' provides the
    faster method.

    cheers
    --
    Tim
    tim at johnsons-web dot com or akwebsoft dot com
    http://www.akwebsoft.com

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJun 20, '11 at 6:14p
activeJun 20, '11 at 10:39p
posts6
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase