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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:07:25+00:00 2026-05-15T06:07:25+00:00

Beginner here! Forgive me in advance for raising what is probably an incredibly simple

  • 0

Beginner here! Forgive me in advance for raising what is probably an incredibly simple problem.

I’ve been trying to put together a Python script that runs multiple find-and-replace actions and a few similar things on a specified plain-text file. It works, but from a programming perspective I doubt it works well. How would I best go about optimizing the actions made upon the ‘outtext’ variable? At the moment it’s basically doing a very similar thing four times over…

import binascii
import re
import struct
import sys

infile = sys.argv[1]
charenc = sys.argv[2]
outFile=infile+'.tex'

findreplace = [
('TERM1', 'TERM2'),
('TERM3', 'TERM4'),
('TERM5', 'TERM6'),
]

inF = open(infile,'rb')
s=unicode(inF.read(),charenc)
inF.close()

# THIS IS VERY MESSY.
for couple in findreplace:
    outtext=s.replace(couple[0],couple[1])
    s=outtext

for couple in findreplace:
    outtext=re.compile('Title: (.*)', re.I).sub(r'\\title'+ r'{\1}', s)
    s=outtext

for couple in findreplace:
    outtext=re.compile('Author: (.*)', re.I).sub(r'\\author'+ r'{\1}', s)
    s=outtext

for couple in findreplace:
    outtext=re.compile('Date: (.*)', re.I).sub(r'\\date'+ r'{\1}', s)
    s=outtext
# END MESSY SECTION.

outF = open(outFile,'wb')
outF.write(outtext.encode('utf-8'))
outF.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-05-15T06:07:25+00:00Added an answer on May 15, 2026 at 6:07 am

    For the last three replacements, the for-loop seems to be unnecessary, since the loop body doesn’t use the loop variable. Also it is not necessary to assign the result to a temporary outtext variable, it can be directly assigned to s.

    These changes make the code much more concise:

    for couple in findreplace:
        s = s.replace(couple[0],couple[1])
    
    s = re.compile('Title: (.*)', re.I).sub(r'\\title {\1}', s)
    s = re.compile('Author: (.*)', re.I).sub(r'\\author {\1}', s)
    s = re.compile('Date: (.*)', re.I).sub(r'\\date {\1}', s)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner at Django here, I've been trying to fix this for a long time
Python beginner here, so I apologize if this question has a simple answer. (I
Right I am a complete beginner here, trying to work through various python tutorials
Beginner here trying to get a pipeline working in bash. If somebody can see
JS beginner here. I need help with a script to place different content in
Python beginner here, I have a list of lists and want to refer to
Python beginner here. I have a text file that is sorted into columns: fields
Beginner here, I have a simple question. In Android what would be the best
Yii beginner here. I am facing some problem with how to query the database
OOP beginner here and my problem is when i get to this event :

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.