替换文件中的多个字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了替换文件中的多个字符串相关的知识,希望对你有一定的参考价值。
我想用一个作为参数给出的表示法替换文件中的不同MAC地址表示法:
python mac_replacer.py -m 00:1a:e8:31:71:7f -f sample.txt
应替换与给定MAC地址对应的每个MAC地址。
LOREM 00.1a.e8.31.71.7f非常fb76.03f0.6701胡萝卜,consetetur sadipscing但直径项目001ae831717f。他们的00-1-e8-31-71-7fAt而感到痛苦,只是他们两个人的补贴。让fb7603f06701维罗EOS 001A-e831-717f
应转向:
LOREM 00:1A:E8:31:71:7F非常fb76.03f0.6701胡萝卜,consetetur sadipscing和00:1A:E8:31:71:1408米项目的直径。 00:1A:E8:31:71:他们的7fAt而感到痛苦,只是他们两个人的补贴。让fb7603f06701维罗EOS 00:1A:E8:31:71:7 |
到目前为止,我所做的是将MAC地址从参数解析为转换器,该转换器识别输入并输出我需要在给定文本中找到它们的所有其他符号。但我半失败地取代了它们。
(代码显示给定MAC地址是十六进制表示法的情况;遗漏标识符部分,if语句的加载)
mac = args.mac_address #In this case 00:1a:e8:31:71:7f
colon2 = mac #00:1a:e8:31:71:7f
dot2 = colon2.replace(":",".") # 00.1a.e8.31.71.7f
hyphen2 = colon2.replace(":","-") # 00-1a-e8-31-71-7f
nosymbol = colon2.replace(":","") # 001ae831717f
colon4 = ':'.join(integer[i:i+4] for i in range(0, len(integer), 4)) # 001a:e831:717f
dot4 = colon4.replace(":",".") # 001a.e831.717f
hyphen4 = colon4.replace(":","-") # 001a-e831-717f
with open(args.filename, "rt") as in_put: # File: sample.txt
with open("out.txt", "wt") as out_put:
for line in in_put:
out_put.write(line.replace(nosymbol, mac))
这有效,但我需要多次重复整个构造。有没有更好的解决方案呢?另外,我想将更改写回同一个文件。我尝试过但它似乎不起作用。
将您的模式放在一个列表中并循环遍历它们:
mac = args.mac_address #In this case 00:1a:e8:31:71:7f
colon2 = mac #00:1a:e8:31:71:7f
dot2 = colon2.replace(":",".") # 00.1a.e8.31.71.7f
hyphen2 = colon2.replace(":","-") # 00-1a-e8-31-71-7f
nosymbol = colon2.replace(":","") # 001ae831717f
colon4 = "001a:e831:717f" # integer is missing used your string instead
dot4 = colon4.replace(":",".") # 001a.e831.717f
hyphen4 = colon4.replace(":","-") # 001a-e831-717f
replacethis = [ colon2 , dot2, hyphen2, nosymbol, colon4, dot4, hyphen4]
put ="""Lorem 00.1a.e8.31.71.7f ipsum fb76.03f0.6701 dolor sit amet,
consetetur sadipscing sed 001ae831717f diam voluptua.
00-1a-e8-31-71-7fAt vero eos et accusam et justo duo dolores et ea rebum.
Stet fb7603f06701 clita kasd gubergren 001a-e831-717f"""
for line in put.split("\n"):
for n in replacethis:
line = line.replace(n, mac)
print(line)
输出:
Lorem 00:1a:e8:31:71:7f ipsum fb76.03f0.6701 dolor sit amet,
consetetur sadipscing sed 00:1a:e8:31:71:7f diam voluptua.
00:1a:e8:31:71:7fAt vero eos et accusam et justo duo dolores et ea rebum.
Stet fb7603f06701 clita kasd gubergren 00:1a:e8:31:71:7f
测试:
你的代码错过了integer
,因此colon4
,dot4
,hyphen4
无法构建 - 我使用了注释中的模式 - 并使用print来筛选文件以用于演示目的。
你可以使用re.sub
:
import re
import contextlib
@contextlib.contextmanager
def change_log(filename):
data = open(filename).read()
addresses = re.findall('[a-z0-9]+\.[a-z0-9]+\.[a-z0-9]+\.\d+\.\d+\.[a-z0-9]+|[a-z0-9]+\-[a-z0-9]+\-[a-z0-9]+\-\d+\-\d+\-[a-z0-9]+|[a-z0-9]+\-[a-z0-9]+\-[a-z0-9]+', data)
new_data = re.sub('[a-z0-9]+\.[a-z0-9]+\.[a-z0-9]+\.\d+\.\d+\.[a-z0-9]+|[a-z0-9]+\-[a-z0-9]+\-[a-z0-9]+\-\d+\-\d+\-[a-z0-9]+|[a-z0-9]+\-[a-z0-9]+\-[a-z0-9]+', re.sub('\.', ':', addresses[0]), data)
yield data
f = open(filename, 'w')
f.write(new_data)
f.close()
with change_log('sample.txt') as f:
print(f)
输出:
Lorem 00:1a:e8:31:71:7f ipsum fb76.03f0.6701 dolor sit amet, consetetur sadipscing sed 001ae831717f diam voluptua. 00:1a:e8:31:71:7fAt vero eos et accusam et justo duo dolores et ea rebum. Stet fb7603f06701 clita kasd gubergren 00:1a:e8:31:71:7f
以上是关于替换文件中的多个字符串的主要内容,如果未能解决你的问题,请参考以下文章