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 6351691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:03:15+00:00 2026-05-24T22:03:15+00:00

I cant seem to figure out why I keep getting this buggy output. Input

  • 0

I cant seem to figure out why I keep getting this buggy output.

Input file:

ff1
ff2
ff3
ff10
ff11
ff20
ff21
ff23
gb20
gb10
gh23

Output File:

FF01
FF02
FF03
FF010
FF011
FF020
FF021
FF023
GB020
GB010
GH023

Code:

import os
import sys

#directory is the directory we will work from
directory = "C:\\Users\\user\\Desktop"
os.chdir(directory)

renameWhat = ["ff", "gb", "gh"]

oldFile = open("New Text Document.txt")
buffer = oldFile.read()

for item in renameWhat:
    for i in range(0, 50):
        if i < 10:
            buffer = buffer.replace(item + str(i), item.upper() + "0" + str(i))
        else:
            buffer = buffer.replace(item + str(i), item.upper() + str(i))

outFile = open("test.txt", "w")
outFile.write(buffer)
outFile.close()
oldFile.close()

What needs to happen:

I’m trying to replace ff to uppercase (done), then I need to add zero’s to all numbers that are less than or equal to 9. So I think that if i is less than 10 should be good enough; but it’s not because it’s adding zeroes to everything over 9. I have tried with len(str(i)) to make sure it’s only of length one, but that still does not work; what am I missing?

  • 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-05-24T22:03:16+00:00Added an answer on May 24, 2026 at 10:03 pm

    The reason your original solution doesn’t work is that it isn’t checking that it’s matching a whole line/word/number. So for instance after replacing ff1 with FF01 your buffer is now:

    FF01
    ff2
    ff3
    FF010
    FF011
    ff20
    ff21
    ff23
    gb20
    gb10
    gh23
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't seem to figure out a good way to do this, but it
I can't seem to figure this one out. No matter what I do, I
I keep getting this NPE in my application and I can't seem to get
I can't seem to figure out how to set the default database in Sql
I can't seem to figure out what I am doing wrong here. I publish
I can't seem to figure out how to make a resize drag handle in
I can't seem to figure out how to get the offsetTop of an element
I can't seem to figure out how to correctly select a Multi Select option
I can't seem to figure out how to inject a disabled element. Any help
I can't seem to figure out what's wrong with my function.... I need to

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.