simple parsing help

S

Steve

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
 
P

Paul McGuire

Steve said:
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!
 
W

wes weston

Steve said:
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
 
M

Michael J. Fromberger

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top