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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:06:56+00:00 2026-06-19T03:06:56+00:00

I need to write into a file with Before and after search replacement patterns.

  • 0

I need to write into a file with Before and after search replacement patterns. I have written the below code. I have used function in writing to output file and it worked fine. But i have around 20 such replacement patterns and i feel i am not writing a good code because i need to create functions for all those replacements. Can you please let me know is there any other way in implementing this?

import re

Report_file = open("report.txt", "w")
st = '''<TimeLog>
<InTime='10Azx'>1056789</InTime>
<OutTime='14crg'>1056867</OutTime>
<PsTime='32lxn'>1056935</PsTime>
<ClrTime='09zvf'>1057689</ClrTime>
</TimeLog>'''

def tcnv(str):
     Report_file.write("Previous TS:  " + str + "\n\n")
     v1 = re.search(r"(?i)<clrtime='(\d+\w+)'>", str)
     val1 = v1.group(1)

     v2 = re.search(r"(?i)(<clrtime='(\d+\w+)'>(.*?)</clrtime>)", str)
     val2 = v2.group(3)

     soutval = "<Clzone><clnvl='" + val1 + "'>" + val2 + "</clnvl></Clzone>" 
     Report_file.write("New TS:  " + soutval + "\n")
     return soutval


st = re.sub(r"(?i)(<clrtime='(\d+\w+)'>(.*?)</clrtime>)", lambda m: tcnv(m.group(1)), st)
st = re.sub(r"(?i)<intime='(\d+\w+)'>(.*?)</intime>", "<Izone><Invl='\\1'>\\2</Invl></Izone>", st)
st = re.sub(r"(?i)<outtime='(\d+\w+)'>(.*?)</outtime>", "<Ozone><onvl='\\1'>\\2</onnvl></Ozone>", st)
st = re.sub(r"(?i)<pstime='(\d+\w+)'>(.*?)</pstime>", "<Pszone><psnvl='\\1'>\\2</psnvl
  • 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-19T03:06:57+00:00Added an answer on June 19, 2026 at 3:06 am

    I didn’t see why you put the re.IGNORECASE flag under the form of (?i), so I don’t use it the following solution, and the pattern is written with the uppercased letters where necessary according to your sample

    Note that you should use the with statement to open the files, it would be far better:

    with open('filename.txt','rb') as f:
    
        ch = f.read() 
    

    The answer

     import re
    
     st = '''<InTime='10Azx'>1056789</InTime>
     <OutTime='14crg'>1056867</OutTime>
     <PsTime='32lxn'>1056935</PsTime>
     <ClrTime='09zvf'>1057689</ClrTime>
     '''
    
     d = dict(zip(('InTime','OutTime','PsTime','ClrTime'),
    
                  (('Izone><Invl','/Invl></Izone'),
                   ('Ozone><onvl','/onnvl></Ozone'),
                   ('Pszone><psnvl','/psnvl></Pszone'),
                   ('Clzone><clnvl','/clnvl></Clzone'))
                  )
              )
    
     def ripl(ma,d=d):
          return "<{}='{}'>{}<{}>".format(d[ma.group(1)][0],
                                          ma.group(2),
                                          ma.group(3),
                                          d[ma.group(1)][1])
    
     st2 = re.sub(r"<(InTime|OutTime|PsTime|ClrTime)='(\d+\w+)'>(.*?)</\1>",
                 ripl, st)
    
     print '%s\n\n%s\n' % (st,st2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to write the output of the code I have to a file
I need to write something into a text file's beginning. I have a text
After creating a file and populating data into it, before close, need read part
I need to write data into drive. I have two options: write raw sectors.(_write(handle,
I need to write to a text file using JavaScript. I have a machine
I have some decimal numbers that I need to write to a text file
I have an interesting scenario where I need to inject a File reference into
I need to write a Perl script that pipes input into a Java program.
The application need write file's last modification date. void Dater(String DateFile) { File file
I need to write a C++ code coverage program that takes in another C++

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.