P
ProvoWallis
Hi,
I've always struggled with classes and this one is no exception.
I'm working in an SGML file and I want to renumber a couple of elements
in the hierarchy based on the previous level.
E.g.,
My document looks like this
<level1>A. Title Text
<level2>1. Title Text
<level2>1. Title Text
<level2>1. Title Text
<level1>B. Title Text
<level2>1. Title Text
<level2>1. Title Text
but I want to change the numbering of the second level to sequential
numbers like 1, 2, 3, etc. so my output would look like this
<level1>A. Title Text
<level2>1. Title Text
<level2>2. Title Text
<level2>3. Title Text
<level1>B. Title Text
<level2>1. Title Text
<level2>2. Title Text
This is what I've come up with on my own but it doesn't work. I was
hoping someone could critique this and point me in the right or better
direction.
Thanks,
Greg
###
def Fix(m):
new = m.group(1)
class ReplacePtSubNumber(object):
def __init__(self):
self._count = 0
self._ptsubtwo_re = re.compile(r'<pt-sub2
no=\"[0-9]\">', re.IGNORECASE| re.UNICODE)
# self._ptsubone_re = re.compile(r'<pt-sub1',
re.IGNORECASE| re.UNICODE)
def sub(self, new):
return self._ptsubtwo_re.sub(self._ptsubNum, new)
def _ptsubNum(self, match):
self._count +=1
return '<pt-sub2 no="%s">' % (self._count)
new = ReplacePtSubNumber().sub(new)
return '<pt-sub1%s<pt-sub1' % (new)
data = re.sub(r'(?i)(?m)(?s)<pt-sub1(.*?)<pt-sub1', Fix, data)
I've always struggled with classes and this one is no exception.
I'm working in an SGML file and I want to renumber a couple of elements
in the hierarchy based on the previous level.
E.g.,
My document looks like this
<level1>A. Title Text
<level2>1. Title Text
<level2>1. Title Text
<level2>1. Title Text
<level1>B. Title Text
<level2>1. Title Text
<level2>1. Title Text
but I want to change the numbering of the second level to sequential
numbers like 1, 2, 3, etc. so my output would look like this
<level1>A. Title Text
<level2>1. Title Text
<level2>2. Title Text
<level2>3. Title Text
<level1>B. Title Text
<level2>1. Title Text
<level2>2. Title Text
This is what I've come up with on my own but it doesn't work. I was
hoping someone could critique this and point me in the right or better
direction.
Thanks,
Greg
###
def Fix(m):
new = m.group(1)
class ReplacePtSubNumber(object):
def __init__(self):
self._count = 0
self._ptsubtwo_re = re.compile(r'<pt-sub2
no=\"[0-9]\">', re.IGNORECASE| re.UNICODE)
# self._ptsubone_re = re.compile(r'<pt-sub1',
re.IGNORECASE| re.UNICODE)
def sub(self, new):
return self._ptsubtwo_re.sub(self._ptsubNum, new)
def _ptsubNum(self, match):
self._count +=1
return '<pt-sub2 no="%s">' % (self._count)
new = ReplacePtSubNumber().sub(new)
return '<pt-sub1%s<pt-sub1' % (new)
data = re.sub(r'(?i)(?m)(?s)<pt-sub1(.*?)<pt-sub1', Fix, data)