替换文件中的多个字符串

时间:2018-01-17 15:32:43

标签: python replace

我想用文件中的不同MAC地址符号替换为一个参数:

python mac_replacer.py -m 00:1a:e8:31:71:7f -f sample.txt

应替换与给定MAC地址对应的每个MAC地址。

  

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-7f 在vero eos et accusam et justo duo dolores et ea rebum。 Stet fb7603f06701 clita kasd gubergren 001a-e831-717f

应转向:

  

Lorem 00:1a:e8:31:71:7f ipsum fb76.03f0.6701 dolor sit amet,consetetur sadipscing sed 00:1a:e8:31:71:7f < / strong> diam voluptua。 00:1a:e8:31:71:7f 在vero eos et accusam et justo duo dolores et ea rebum。 Stet fb7603f06701 clita kasd gubergren 00:1a:e8:31:71:7f

到目前为止,我所做的是将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))

这有效,但我需要多次重复整个构造。有没有更好的解决方案呢?另外,我想将更改写回同一个文件。我尝试过,但似乎没有用。

2 个答案:

答案 0 :(得分:0)

将您的模式放在一个列表中并循环显示它们:

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,因此colon4dot4hyphen4无法构建 - 我使用了评论中的模式 - 而是使用print to screen用于演示目的的文件。

答案 1 :(得分:0)

您可以使用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