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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:37:32+00:00 2026-05-23T10:37:32+00:00

I am new to python so excuse my ignorance. Currently, I have a text

  • 0

I am new to python so excuse my ignorance.

Currently, I have a text file with some words marked as <>.

My goal is to essentially build a script which runs through a text file with such marked words. Each time the script finds such a word, it would ask the user for what it wants to replace it with.

For example, if I had a text file:

Today was a <<feeling>> day.

The script would run through the text file so the output would be:

Running script...
feeling? great
Script finished.

And generate a text file which would say:

Today was a great day.

Advice?

Edit: Thanks for the great advice! I have made a script that works for the most part like I wanted. Just one thing. Now I am working on if I have multiple variables with the same name (for instance, “I am <>. Bob is also <>.”) the script would only prompt, feeling?, once and fill in all the variables with the same name.

Thanks so much for your help again.

  • 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-23T10:37:32+00:00Added an answer on May 23, 2026 at 10:37 am

    Basically the same solution as that offerred by @phihag, but in script form

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import argparse
    import re
    from os import path
    
    pattern = '<<([^>]*)>>'
    
    def user_replace(match):
        return raw_input('%s? ' % match.group(1))
    
    
    def main():
        parser = argparse.ArgumentParser()
        parser.add_argument('infile', type=argparse.FileType('r'))
        parser.add_argument('outfile', type=argparse.FileType('w'))
        args = parser.parse_args()
    
        matcher = re.compile(pattern)
    
        for line in args.infile:
            new_line = matcher.sub(user_replace, line)
            args.outfile.write(new_line)
    
        args.infile.close()
        args.outfile.close()
    
    if __name__ == '__main__':
        main()
    

    Usage: python script.py input.txt output.txt

    Note that this script does not account for non-ascii file encoding.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to python programming and need some help with some basic file I/O
Excuse my ignorance, I'm very new to Python. I'm trying to perform factor analysis
I am rather new to wx/python so please excuse if this is stupid or
I'm new to python, so please excuse what is probably a pretty dumb question.
I am new to Python, and I'm working on writing some database code using
I am really new to Python and I have been looking for an example
I am a newcomer to the Python and Twisted game so excuse the ignorance
New to python and trying to learn the ropes of file i/o. Working with
I am new to python and numpy so please excuse me if this problem
I am new to Django/Python, so excuse me if my question is straightforwardly obvious!

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.