在多个文本文件中查找 IP 地址,并使用正则表达式将其替换为 Python 中的另一个字符串
Find IP address in multiple text files and replace it with another string in Python with regex
我有多个文本文件如下:
我想读取所有文本文件并在每个文件中找到 IP 值,并将其替换为文本中可用的 noresult 字符串,并将每个文件保存在 python.
文字1
Id = 0005
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
result = noresult
Id = 0006
Cause = FAILURE
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4 result = noresult
ip[10.1.1.1
文字2
Id = 0007
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
Id = 0008
Cause = FAILURE
result = noresult
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4
result = noresult
ip[10.1.1.3
需要的结果:
文字1
Id = 0005
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
result = 10.1.1.1
Id = 0006
Cause = FAILURE
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4 result = 10.1.1.1
ip[10.1.1.1
文字2
Id = 0007
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
Id = 0008
Cause = FAILURE
result = 10.1.1.3
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4
result = 10.1.1.3
ip[10.1.1.3
如果 ip 始终是您的最后一个字段,您可以简单地这样做:
txt = txt.replace("noresult", txt.split("ip[")[-1])
详细的,如果你想读、改、写:
txt = open("filename.txt", "r").read()
txt = txt.replace("noresult", txt.split("ip[")[-1])
open("filename.txt", "w").write(txt)
如果你有更多的文件,你可以将它们的路径分组在一个列表中file_list = ["file1.txt", "file2.txt", ... ]
,然后在这个列表上循环重复上述过程:
for filepath in file_list:
txt = open(filepath, "r").read()
txt = txt.replace("noresult", txt.split("ip[")[-1])
open(filepath, "w").write(txt)
我有多个文本文件如下:
我想读取所有文本文件并在每个文件中找到 IP 值,并将其替换为文本中可用的 noresult 字符串,并将每个文件保存在 python.
文字1
Id = 0005
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
result = noresult
Id = 0006
Cause = FAILURE
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4 result = noresult
ip[10.1.1.1
文字2
Id = 0007
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
Id = 0008
Cause = FAILURE
result = noresult
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4
result = noresult
ip[10.1.1.3
需要的结果:
文字1
Id = 0005
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
result = 10.1.1.1
Id = 0006
Cause = FAILURE
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4 result = 10.1.1.1
ip[10.1.1.1
文字2
Id = 0007
Cause = ERROR
Code = 307
Event Time = 2020-11-09 10:16:48
Severity = WARNING
Severity Code = 5
Id = 0008
Cause = FAILURE
result = 10.1.1.3
Code = 517
Event Time = 2020-11-09 10:19:47
Severity = MINOR
Severity Code = 4
result = 10.1.1.3
ip[10.1.1.3
如果 ip 始终是您的最后一个字段,您可以简单地这样做:
txt = txt.replace("noresult", txt.split("ip[")[-1])
详细的,如果你想读、改、写:
txt = open("filename.txt", "r").read()
txt = txt.replace("noresult", txt.split("ip[")[-1])
open("filename.txt", "w").write(txt)
如果你有更多的文件,你可以将它们的路径分组在一个列表中file_list = ["file1.txt", "file2.txt", ... ]
,然后在这个列表上循环重复上述过程:
for filepath in file_list:
txt = open(filepath, "r").read()
txt = txt.replace("noresult", txt.split("ip[")[-1])
open(filepath, "w").write(txt)