Re: How to use python regular expression to substitute string value

2006-02-26 Thread Pasi Savolainen
[EMAIL PROTECTED] writes: > When I try your idea, I have this error > > x, y, width, height = re.findall(pattern, str)[0] > IndexError: list index out of range > > How can I use findall to handle error case? i.e. what if there is no > match? how can I handle it gracefully? This is explained i

Re: How to use python regular expression to substitute string value

2006-02-26 Thread Allerdyce . John
When I try your idea, I have this error x, y, width, height = re.findall(pattern, str)[0] IndexError: list index out of range How can I use findall to handle error case? i.e. what if there is no match? how can I handle it gracefully? -- http://mail.python.org/mailman/listinfo/python-list

Re: How to use python regular expression to substitute string value

2006-02-26 Thread Crutcher
Are you sure node.data contains newlines? You could try just: print node.data print node.data.split('\n') This should give you an idea. From the interpreter: >>> s = """ ... abc ... def ... xyz""" >>> s.split('\n') ['', 'abc', 'def', 'xyz'] -- http://mail.python.org/mailman/listinfo/python-list

Re: How to use python regular expression to substitute string value

2006-02-26 Thread Allerdyce . John
okay, but I have a simpler question, how can I split using only "\n"? I try this: strings = node.data.split("\n"); print node.data for str in strings: print str where node.data has multiple lines, but in the for loop, I don't see str gets pr

Re: How to use python regular expression to substitute string value

2006-02-26 Thread Crutcher
You don't really need regexes for this. Assuming there is no whitespace in any of your values, it should be really easy to parse the string. s = 'x:11 y:0 w:760 h:19 area:14440 areaPerCent:0 totalAreaPerCent:-3.08011e+16 type:3 path:///-/1/1' s.split() # break the string on whitespace > ['x:11',

Re: How to use python regular expression to substitute string value

2006-02-26 Thread Allerdyce . John
Thanks. But i don't understand why I need to do this: x. . area = map (lambda x: int(x), re.findall (pattern, line)[0] if i have the value already by doing this: x, y, width, height, area = re.findall(pattern, line)[0] print "rect x=\"%(x)s\" y=\"%(y)s\" width=\"%(width)s\" height=\"%(height)s\"

Re: How to use python regular expression to substitute string value

2006-02-26 Thread Pasi Savolainen
[EMAIL PROTECTED] writes: > Hi, > I am new to python. I would like to know how to use python regular > expression to substitute string value? > I have an input string like this: > x:11 y:0 w:760 h:19 area:14440 areaPerCent:0 > totalAreaPerCent:-3.08011e+16 type:3 path:///-/1

How to use python regular expression to substitute string value

2006-02-26 Thread Allerdyce . John
Hi, I am new to python. I would like to know how to use python regular expression to substitute string value? I have an input string like this: x:11 y:0 w:760 h:19 area:14440 areaPerCent:0 totalAreaPerCent:-3.08011e+16 type:3 path:///-/1/1 and I would like to convert it to: rect x="11&