[英]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. 但是我大约有20种这样的替换模式,我觉得我写的代码不好,因为我需要为所有这些替换创建函数。 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
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 我不明白为什么将re.IGNORECASE标志放在(?i)
形式下,所以我没有在以下解决方案中使用它,根据您的示例,该模式在必要时用大写字母书写
Note that you should use the with
statement to open the files, it would be far better: 请注意,您应该使用with
语句打开文件,这样会更好:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.