Fla-Hyd
Fla-Hyd

Reputation: 277

Search Patterns replacement using lambda

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

Upvotes: 0

Views: 80

Answers (1)

eyquem
eyquem

Reputation: 27585

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)

Upvotes: 1

Related Questions