String manipulation using RE

aditya shukla adityashukla1983 at gmail.com
Mon Oct 27 01:56:21 CET 2008


Hello folks, i have a string

eg

"(((A:1,B:1):3,C:3):4,((E:1,F:1):2,D:2):4)"

now i have to convert this string to

"(((A:1,B:1):2,C:3):1,((E:1,F:1):1,D:2):2)"
So i used the logic eg. taking the substring "1):3" and converting it to
"1):2"(3-1=2) so on for all the similar substrings.But i am not able to
replace them back into the original string.
This is what my code looks like

import re

str = "(((A:1,B:1):3,C:3):4,((E:1,F:1):2,D:2):4)"

p =re.compile('\d\):\d') # compiling an re object

list1 =[]

iterator = p.finditer(str)

for match in iterator:

       x = match.group()

       l = x.split('):')

       value = int(l[1])-int(l[0])

       z= repr(value)

       rm = l[1]

       x= x.rstrip(rm)

       y = x + repr(value)

      print y

I am able to get the desired output further, any help is appreciated

Thanks

Aditya
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081026/00cdc7ec/attachment.html>


More information about the Python-list mailing list