Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:10:30+00:00 2026-06-10T18:10:30+00:00

this is my python script below, I try to use sub() to replace all

  • 0

this is my python script below, I try to use sub() to replace all the values between <StartNum> and </StartNum> by using a variable continous number n Then, write the results to a new text file.
any people could point out the mistake?

import re
f1 = open('name.xml', 'r')
f2 = open('result.txt', 'w')
data=f1.read()
n=5000
for line in f1:
      f2.write(re.sub('<StartNum>(.*)</StartNum>', r'<StartNum>%s</StartNum>' % str(n), data))
      if "<StartNum>" in line:
          n=n+1
f1.close() 
f2.close()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:10:32+00:00Added an answer on June 10, 2026 at 6:10 pm

    You have a few problems with your code.

    1. You are reading the file, f1, into data but you are continuing to us f1 for the loop. Once you have read the data, you can close the file.
    2. You would probably want to split the lines up. You can do this a number of ways. The readlines method is probably the most straight forward.
    3. When you are evaluating a regex in a loop, it can be very slow because it has to be recompiled each iteration. Instead, you can compile the regex ahead of time and use it instead.
    4. In your re.sub, you were using data as the source, while you actually want the line.

    There are other things you could do to improve the code, but the ones listed above are pretty necessary.

    I am guessing this will likely do what you want.

    import re
    f1 = open('name.xml', 'r')
    f2 = open('result.txt', 'w')
    data = f1.readlines()
    f1.close()
    n = 5000
    rex = re.compile('<StartNum>(.*)</StartNum>')
    for line in data:
        f2.write(rex.sub(r'<StartNum>%s</StartNum>' % str(n), line))
        if "<StartNum>" in line:
            n += 1
    f2.close()
    

    Given names.xml of

    <root>
        <StartNum>1</StartNum>
        <StartNum>5</StartNum>
        <StartNum>8</StartNum>
        <StartNum>9</StartNum>
        <StartNum>13</StartNum>
        <StartNum>33</StartNum>
        <foo>
            <bar baz="5" />
        </foo>
    </root>
    

    result.txt will be:

    <root>
        <StartNum>5000</StartNum>
        <StartNum>5001</StartNum>
        <StartNum>5002</StartNum>
        <StartNum>5003</StartNum>
        <StartNum>5004</StartNum>
        <StartNum>5005</StartNum>
        <foo>
            <bar baz="5" />
        </foo>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

System: Mac OSX 10.6.5, Python 2.6 I try to run the python script below:
Hey brilliant minds out there, I'm trying to get this Python script that I
I have some questions about the performance of this simple python script: import sys,
This is the first Python script I've tried to create. I'm reading a xml
I have a python script the runs this code: strpath = sudo svnadmin create
I'm working on a Python script that transforms this: foo bar Into this: [[Component
In my editor (notepad++) in Python script edit mode, a line ## is this
I have a Python script that is running a few ls commands. This script
I tried running a python script: print Hello, World! And I get this error:
I am trying to write a python script that takes record data like this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.