Newbie Count Question

ProvoWallis gshepherd281 at
Mon Oct 10 02:43:04 CEST 2005

I have a newbie count question.

I have a number of SGML documents divided into sections but over the
course of editing them the some sections have been deleted (and perhaps
others added). I'd like to renumber them. The input documents look like

<sec-main no="1.01">
<sec-main no="1.02">
<sec-main no="1.03">
<sec-main no="1.08">
<sec-main no="1.09">
<sec-main no="1.10">
<sec-main no="1.15">

and after renumbering I would like the sections to look like this:

<sec-main no="1.01">
<sec-main no="1.02">
<sec-main no="1.03">
<sec-main no="1.04">
<sec-main no="1.05">
<sec-main no="1.06">
<sec-main no="1.07">

so they are basically numbered sequentially from 1 thru to the end of
the number of sections.

I've managed to get this far thanks to looking at other posts on the
board but no matter waht I try all of the sections end up being
numbered for the total number of sections in the document. e.g., if
there are 100 sections in the document the "no" attribute is "1.100"
for each one.

import os, re

setpath = raw_input("Enter the path where the program should run: ")

for root, folders, files in os.walk(setpath):
    for name in files:
        filepath = os.path.join(root, name)
        fileopen = open(filepath, 'r')
        data =

        secmain_pattern = re.compile(r'<sec-main no=\"(\d*)\.(\d*)\">',
        m =
        all = secmain_pattern.findall(data)

        counter = 0
        for i in range(0,len(all)):
             counter = counter + 1
             print counter

        if m is not None:
             def new_number(match):
                  return '<sec-main no="%s.%s">' % (,
             data = secmain_pattern.sub(new_number, data)

        outputFile = file(os.path.join(root,name), 'w')

Thanks for your help!

More information about the Python-list mailing list