simple parsing help

Discussion in 'Python' started by Steve, Jul 24, 2004.

  1. Steve

    Steve Guest

    Hi,

    I have a string that looks kinda like "Winner (121,10) blah blah blah
    Winner (10,400) blah blah Winner (103,4) blah blah..."

    I just want to extract the coordinate parts of the string (xxx,xxx)
    which could be 1-3 characters and put them into an array ideally.
    What is the easiest way to do this in Python? Can anyone provide a
    simple line or two of code to help me get started?

    Thanks very much,

    Steve
     
    Steve, Jul 24, 2004
    #1
    1. Advertising

  2. Steve

    Paul McGuire Guest

    "Steve" <> wrote in message
    news:...
    > Hi,
    >
    > I have a string that looks kinda like "Winner (121,10) blah blah blah
    > Winner (10,400) blah blah Winner (103,4) blah blah..."
    >
    > I just want to extract the coordinate parts of the string (xxx,xxx)
    > which could be 1-3 characters and put them into an array ideally.
    > What is the easiest way to do this in Python? Can anyone provide a
    > simple line or two of code to help me get started?
    >
    > Thanks very much,
    >
    > Steve

    Here's a pyparsing version, which will be slower than re's, but perhaps a
    bit more readable.
    Download pyparsing at http://pyparsing.sourceforge.net.
    -- Paul


    from pyparsing import Word, nums, delimitedList, Suppress

    testdata = "Winner (121,10) blah blah blah Winner (10,400) blah blah Winner
    (103,4) blah blah"

    num = Word(nums)
    coord = Suppress("(") + num + Suppress(",") + num + Suppress(")")
    for t,s,e in coord.scanString(testdata):
    print t

    gives:
    ['121', '10']
    ['10', '400']
    ['103', '4']


    Or if you like, add this line just before the for statement to do
    conversions to int as well:

    coord.setParseAction( lambda s,l,t: map(int,t) )

    Giving:

    [121, 10]
    [10, 400]
    [103, 4]

    Now change 'print t' to 'print "x=%d, y=%d" % tuple(t)' and you'll get:
    x=121, y=10
    x=10, y=400
    x=103, y=4


    Have fun!
     
    Paul McGuire, Jul 25, 2004
    #2
    1. Advertising

  3. Steve

    wes weston Guest

    Steve wrote:
    > Hi,
    >
    > I have a string that looks kinda like "Winner (121,10) blah blah blah
    > Winner (10,400) blah blah Winner (103,4) blah blah..."
    >
    > I just want to extract the coordinate parts of the string (xxx,xxx)
    > which could be 1-3 characters and put them into an array ideally.
    > What is the easiest way to do this in Python? Can anyone provide a
    > simple line or two of code to help me get started?
    >
    > Thanks very much,
    >
    > Steve


    Steve,
    Here's a long hand version (that makes assumptions).
    wes
    --------------------------------------------------------------
    str = "Winner (121,10) blah blah blah Winner (10,400) blah blah Winner (103,4) blah blah"

    list = str.split()
    pts = []
    for s in list:
    if (s[0] == '(' ) and (s[-1]== ')' ):
    try:
    temp = s[1:-1].split(',')
    pts.append( (int(temp[0]), int(temp[1])) )
    except:
    pass

    for p in pts:
    print p[0],p[1]

    ---------------------------------------------------


    produces:
    >>>

    121 10
    10 400
    103 4
    >>>
     
    wes weston, Jul 25, 2004
    #3
  4. In article <>,
    (Steve) wrote:

    > Hi,
    >
    > I have a string that looks kinda like "Winner (121,10) blah blah blah
    > Winner (10,400) blah blah Winner (103,4) blah blah..."
    >
    > I just want to extract the coordinate parts of the string (xxx,xxx)
    > which could be 1-3 characters and put them into an array ideally.
    > What is the easiest way to do this in Python? Can anyone provide a
    > simple line or two of code to help me get started?
    >
    > Thanks very much,
    >
    > Steve


    Hello, Steve,

    You've gotten a couple of answers to this already, but I'd like to offer
    another possibility, which makes use of Python's expressive iterator and
    list comprehension features:

    import re

    w_expr = re.compile('Winner \((\d{1,3}),\s*(\d{1,3})\)')

    def parse_winners(s):
    return [ ( m.group(1), m.group(2) ) for m in w_expr.finditer(s) ]

    As you can see, it's concise and not too hard to read.

    Cheers,
    -M

    --
    Michael J. Fromberger | Lecturer, Dept. of Computer Science
    http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
     
    Michael J. Fromberger, Jul 25, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GIMME
    Replies:
    2
    Views:
    877
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    585
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    612
    Christopher Diggins
    Jul 9, 2007
  4. Christopher Diggins
    Replies:
    0
    Views:
    439
    Christopher Diggins
    Jul 9, 2007
  5. Replies:
    14
    Views:
    527
Loading...

Share This Page