我有一个文本文件,其中包含一个唯一的文本,如vrf_name:** nVSatellite,该文本已存储在字典中。在这两者之间,一个vrf_name到即将到来的vrf_name包含一组我需要在列表中解析的行 请为我提供解决方案。
VRF:** nVSatellite
L 10.61.0.252/32 is directly connected, 9w5d, Bundle-Ether100.120
C 10.61.76.240/31 is directly connected, 9w5d, Bundle-Ether50.120
L 10.61.76.240/32 is directly connected, 9w5d, Bundle-Ether50.120
O 223.224.150.16/32 [110/12] via 10.61.0.230, 5w4d, Bundle-Ether200.122
VRF:ePDG-IN
L 10.61.0.252/32 is directly connected, 9w5d, Bundle-Ether100.120
C 10.61.76.240/31 is directly connected, 9w5d, Bundle-Ether50.120
L 10.61.76.240/32 is directly connected, 9w5d, Bundle-Ether50.120
O 223.224.150.16/32 [110/12] via 10.61.0.230, 5w4d, Bundle-Ether200.122
VRF:JKIT-TL
L 10.61.0.252/32 is directly connected, 9w5d, Bundle-Ether100.120
C 10.61.76.240/31 is directly connected, 9w5d, Bundle-Ether50.120
L 10.61.76.240/32 is directly connected, 9w5d, Bundle-Ether50.120
O 223.224.150.16/32 [110/12] via 10.61.0.230, 5w4d, Bundle-Ether200.122
VRF:MST-LKAS
.
.
.
.
dict = {}
for index, line in enumerate(read_file()):
match1 = re.search(r"VRF:", line)
if match1:
dict[index] = line
预期结果: VRF之间存在一行:需要在列表中进行解析
答案 0 :(得分:0)
import re
d = {}
with open('vrf.txt') as file:
lines = file.readlines()
expr = re.compile(r'VRF:')
first_match = None
first_index = -1
for index, line in enumerate(lines):
next_match = expr.match(line)
if next_match:
if first_match:
d[first_index] = lines[first_index: index]
first_match = next_match
first_index = index
答案 1 :(得分:0)
不清楚您的输入文件是什么样子,以及您想从中保存什么。
顺便说一句,您要避免使用dict
作为变量名。这是内置类型。
假设这样的输入-vrf_textfile.txt
:
VRF: **nVSatellite
L 10.61.0.252/32 is directly connected, 9w5d, Bundle-Ether100.120
C 10.61.76.240/31 is directly connected, 9w5d, Bundle-Ether50.120
L 10.61.76.240/32 is directly connected, 9w5d, Bundle-Ether50.120
O 223.224.150.16/32 [110/12] via 10.61.0.230, 5w4d, Bundle-Ether200.122
VRF: ePDG-IN
L 10.61.0.252/32 is directly connected, 9w5d, Bundle-Ether100.120
C 10.61.76.240/31 is directly connected, 9w5d, Bundle-Ether50.120
L 10.61.76.240/32 is directly connected, 9w5d, Bundle-Ether50.120
O 223.224.150.16/32 [110/12] via 10.61.0.230, 5w4d, Bundle-Ether200.122
VRF: JKIT-TL
L 10.61.0.252/32 is directly connected, 9w5d, Bundle-Ether100.120
C 10.61.76.240/31 is directly connected, 9w5d, Bundle-Ether50.120
L 10.61.76.240/32 is directly connected, 9w5d, Bundle-Ether50.120
O 223.224.150.16/32 [110/12] via 10.61.0.230, 5w4d, Bundle-Ether200.122
代码:
with open(r"vrf_textfile.txt") as f:
vrf_text = f.read() # This is read in as a single long string
vrf = {}
current_key = None
for index, line in enumerate(vrf_text.splitlines()):
if line.startswith("VRF:"):
vrf[index] = []
current_key = index
elif current_key is not None:
if line.strip() != "": # I assume you don't want to include blank lines
vrf[current_key].append(line)
# vrf is a dictionary of lists. Every key is a line number (int)
for line_number, lists in vrf.items():
for item in lists:
print(f"{line_number} - '{item}'")
输出:
0 - ' L 10.61.0.252/32 is directly connected, 9w5d, Bundle-Ether100.120'
0 - ' C 10.61.76.240/31 is directly connected, 9w5d, Bundle-Ether50.120'
0 - ' L 10.61.76.240/32 is directly connected, 9w5d, Bundle-Ether50.120'
0 - ' O 223.224.150.16/32 [110/12] via 10.61.0.230, 5w4d, Bundle-Ether200.122'
6 - ' L 10.61.0.252/32 is directly connected, 9w5d, Bundle-Ether100.120'
6 - ' C 10.61.76.240/31 is directly connected, 9w5d, Bundle-Ether50.120'
6 - ' L 10.61.76.240/32 is directly connected, 9w5d, Bundle-Ether50.120'
6 - ' O 223.224.150.16/32 [110/12] via 10.61.0.230, 5w4d, Bundle-Ether200.122'
12 - ' L 10.61.0.252/32 is directly connected, 9w5d, Bundle-Ether100.120'
12 - ' C 10.61.76.240/31 is directly connected, 9w5d, Bundle-Ether50.120'
12 - ' L 10.61.76.240/32 is directly connected, 9w5d, Bundle-Ether50.120'
12 - ' O 223.224.150.16/32 [110/12] via 10.61.0.230, 5w4d, Bundle-Ether200.122'