simple parsing help

wes weston wweston at att.net
Sun Jul 25 15:20:00 CEST 2004


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
 >>>




More information about the Python-list mailing list