[英]Remove entire line in text file that contain with specific word/string in python
所有。 我使用SO中的示例并尝试删除文本文件中的几行/字符串但不成功。 例如,需要删除的字符串行
OSPF Process 1 with Router ID 1.1.1.1
Area: 0.0.0.11
Link State Database
我能够通过如下指定整个字符串/行来删除这些行,但这一次只能删除一行而另一个问题是路由器ID和区域可以是任何数字并动态更改。
filename = 'raw.txt'
with open(filename, 'r') as fin:
lines = fin.readlines()
with open('clean.txt', 'w') as fout:
for line in lines:
if 'Area: 0.0.0.10' not in line:
fout.write(line)
我尝试使用startwith但它不会删除它。
if not line.startswith('OSPF'):
这是文本文件中的外观和字符串放置方式。 OSPF ...,Area ...,Link ...行不从左边开始,它以空格开头,所以我认为这就是为什么startswith不起作用的原因。
OSPF Process 1 with Router ID 1.1.1.1
Area: 0.0.0.11
Link State Database
some textxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
OSPF Process 1 with Router ID 2.1.1.1
Area: 0.0.0.12
Link State Database
some textxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
OSPF Process 1 with Router ID 2.2.2.2
Area: 0.0.0.33
Link State Database
some textxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
删除这些行后,预计如下所示
some textxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
some textxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
some textxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
请进一步指教,谢谢
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
OSPF Process 1 with Router ID 1.1.1.1
Area: 0.0.0.11
Link State Database
例如,执行脚本时超过5行。它将删除3行,但仍然保留2行
另一个例子
* Link ID: 10.1.155.20
Data : 255.255.255.252
Link Type: StubNet
Metric : 1
Priority : Low
Area: 0.0.0.13
Link State Database
Type : Router
Ls id : 1.4.0.2
Adv rtr : 1.4.0.2
这有4行(在Area之前和Type之前)...当执行脚本时只有2行删除...而2将保留....对于此...最终应该如下
* Link ID: 10.1.155.20
Data : 255.255.255.252
Link Type: StubNet
Metric : 1
Priority : Low
Type : Router
Ls id : 1.4.0.2
Adv rtr : 1.4.0.2
删除特定的字符串和行以及下一行(在链接状态数据库行之后)
clean.txt
**To remove this empty line
To remove this empty line
To remove this empty line**
Type : Router
Ls id : 1.4.0.1
Adv rtr : 1.4.0.1
Ls age : 996
Len : 48
Options : ASBR E
seq# : 8000002f
chksum : 0xe7f5
Link count: 2
* Link ID: 1.16.9.9
Data : 10.1.155.2
Link Type: P-2-P
Metric : 100
* Link ID: 10.1.155.20
Data : 255.255.255.252
Link Type: StubNet
Metric : 100
Priority : Low
Type : Router
Ls id : 1.16.9.9
Adv rtr : 1.16.9.9
Ls age : 392
Len : 48
Options : ABR E
seq# : 8000001e
chksum : 0x3116
Link count: 2
* Link ID: 1.4.0.1
Data : 10.242.177.21
Link Type: P-2-P
Metric : 1
* Link ID: 10.1.155.20
Data : 255.255.255.252
Link Type: StubNet
Metric : 1
Priority : Low
**To remove this empty line**
Type : Router
Ls id : 1.4.0.2
Adv rtr : 1.4.0.2
Ls age : 1194
Len : 96
Options : ASBR E
seq# : 8001cf7b
chksum : 0xbfae
Link count: 6
* Link ID: 1.4.0.2
Data : 255.255.255.255
Link Type: StubNet
Metric : 0
Priority : Medium
* Link ID: 1.4.0.1
Data : 10.0.0.2
Link Type: P-2-P
Metric : 10
* Link ID: 10.0.0.0
Data : 255.255.255.252
Link Type: StubNet
Metric : 10
Priority : Low
* Link ID: 10.40.8.0
Data : 255.255.255.252
Link Type: StubNet
Metric : 100
Priority : Low
* Link ID: 19.23.23.15
Data : 10.40.10.130
Link Type: P-2-P
Metric : 10
* Link ID: 1.4.10.200
Data : 255.255.255.252
Link Type: StubNet
Metric : 10
Priority : Low
To remove this empty line
Type : Router
Ls id : 100.100.0.10
Adv rtr : 100.100.0.10
Ls age : 171
Len : 84
Options : ASBR E
seq# : 8001a292
chksum : 0x5fa2
Link count: 5
* Link ID: 100.100.0.10
Data : 255.255.255.255
Link Type: StubNet
Metric : 12
Priority : Medium
* Link ID: 10.10.0.1
Data : 10.10.10.18
Link Type: P-2-P
Metric : 10
* Link ID: 10.10.10.17
Data : 255.255.255.255
Link Type: StubNet
Metric : 10
Priority : Medium
* Link ID: 19.23.23.15
Data : 10.10.30.30
Link Type: P-2-P
Metric : 10
* Link ID: 10.90.25.30
Data : 255.255.255.255
Link Type: StubNet
Metric : 10
Priority : Medium
Type : Router
Ls id : 10.10.0.1
Adv rtr : 10.10.0.1
Ls age : 191
Len : 96
Options : ASBR E
seq# : 80013bcf
chksum : 0x9871
Link count: 6
* Link ID: 10.10.0.1
Data : 255.255.255.255
Link Type: StubNet
Metric : 12
Priority : Medium
* Link ID: 15.51.51.14
Data : 10.10.0.130
Link Type: P-2-P
Metric : 10
* Link ID: 10.10.0.129
Data : 255.255.255.255
Link Type: StubNet
Metric : 10
Priority : Medium
* Link ID: 100.100.0.10
Data : 10.10.10.17
Link Type: P-2-P
Metric : 10
* Link ID: 10.10.10.18
Data : 255.255.255.255
Link Type: StubNet
Metric : 10
Priority : Medium
* Link ID: 16.16.16.0
Data : 255.255.255.252
Link Type: StubNet
Metric : 10
Priority : Low
Type : Router
Ls id : 15.51.51.14
Adv rtr : 15.51.51.14
Ls age : 2487
Len : 60
Options : ASBR ABR E
seq# : 8000003c
chksum : 0x1714
Link count: 3
* Link ID: 10.242.95.12
Data : 255.255.255.252
Link Type: StubNet
Metric : 1
Priority : Low
* Link ID: 10.10.0.1
Data : 10.10.0.129
Link Type: P-2-P
Metric : 1
* Link ID: 10.10.0.128
Data : 255.255.255.252
Link Type: StubNet
Metric : 1
Priority : Low
**To remove this empty line
To remove this empty line**
请注意,该行不是以OSPF
开头,而是以一堆空格开始, 然后是 OSPF。 尝试先strip
线。 此外, startswith
可以采用可能的前缀元组,因此您可以一次性检查所有内容。
for line in lines:
if not line.strip().startswith(("OSPF", "Area", "Link State")):
fout.write(line)
请注意,如果实际文本中的某些行也以Area
或类似的方式开头,则可能会失败。
您还可以使用正则表达式来确保该行必须以某些空格开头, 然后使用其中一个关键字:
import re
for line in lines:
if not re.match(r"\s+(Area|OSPF|Link State)", line):
fout.write(line)
您可以使用正则表达式查找某些特定文本并将其删除。 下面是示例代码,您可以根据您的要求使用不同的正则表达式。
尝试以下代码:
import re
regex = "OSPF|Area|Link"
for line in lines:
if not re.findall(regex, line):
print line
你可以做什么而不是逐行阅读是读取文本文件的整个内容,并使用特定匹配的模式考虑可能变化的数字部分。
^[ \t]*OSPF Process \d+ with Router ID \d+(?:\.\d+){3}\s*Area: \d+(?:\.\d+){3}\s*Link State Database\s*(?:\n|$)
说明
^
字符串的开始 [ \\t]*
匹配空格或制表符的0倍以上 OSPF Process \\d+ with Router ID \\d+(?:\\.\\d+){3}
匹配文本,将进程和路由器ID的数字\\d+
格式考虑在内 \\s*Area: \\d+(?:\\.\\d+){3}
匹配Area:
后跟1位数字,重复3次点和1位数字 \\s*Link State Database
匹配空白字符和文字文本的0+倍 \\s*(?:\\n|$)
匹配空白字符串的0+次,然后匹配换行符或断言字符串的结尾 例如:
import re
filename = 'raw.txt'
pattern = r"^[ \t]*OSPF Process \d+ with Router ID \d+(?:\.\d+){3}\s*Area: \d+(?:\.\d+){3}\s*Link State Database\s*(?:\n|$)"
with open(filename, 'r') as fin:
res = re.sub(pattern, "", fin.read(), 0, re.MULTILINE)
text_file = open("clean.txt", "w")
text_file.write(res)
text_file.close()
编辑
要匹配后面的空换行符,可以在数据库后使用add:
[ \\t]*
匹配空格或字符串的0+倍 (?:
非捕获组
(?:\\r?\\n|\\r)[ \\t]*
匹配换行符,然后匹配标签或空格的0+次 )?
关闭非捕获组并使其可选 $
断言字符串的结尾 完整模式:
^[ \t]*OSPF Process \d+ with Router ID \d+(?:\.\d+){3}\s*Area: \d+(?:\.\d+){3}\s*Link State Database[ \t]*(?:(?:\r?\n|\r)[ \t]*)?$
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.