我需要使用python将日志文件转换成字典格式
show_pcs =
1 Po1(SU) Eth LACP Eth1/24(P) Eth2/24(P) Eth3/24/10(P)
Eth4/24(P)
2 Po2(SU) Eth LACP Eth1/1/1(P) Eth1/1/2(P) Eth1/1/3(P)
Eth1/1/4(P) Eth2/1/1(P) Eth2/1/2(P)
Eth2/1/3(P) Eth2/1/4(P)
3 Po3(SD) Eth NONE --
4 Po4(SD) Eth NONE --
5 Po5(SD) Eth LACP Eth1/3/1(P) Eth1/3/2(P) Eth1/3/3(P)
Eth101/3/4(D) Eth2/3/1(P) Eth2/3/2(P)
Eth2/3/3(P) Eth2/3/4(D)
6 Po6(SU) Eth LACP Eth1/14/1(P) Eth1/14/2(P) Eth1/14/3(P)
Eth1/14/4(P) Eth102/14/1(P) Eth2/14/2(P)
Eth2/14/3(P) Eth2/14/4(P)
7 Po7(SD) Eth LACP Eth1/22(P) Eth2/22(P) Eth3/22(P)
Eth107/1/22(D)
8 Po8(SU) Eth LACP Eth1/23(P) Eth2/23(P) Eth3/23(P)
d_t = {}
pattern_1= 'Po\d+'
pattern_2='Eth\d+\/\d+(?:\/\d+)?\((?:P|D)\)'
result_1 = re.findall(pattern_1,show_pcs)
result_2 = re.findall(pattern_2,show_pcs)
for p1 in result_1:
for p2 in result_2:
d_t[result_1] = result_2
print(d_t)
{ 'Po1' : ['Eth1/24(P)', 'Eth2/24(P)', 'Eth3/24/10(P)','Eth4/24(P)'], 'Po2': ....}
一种直接的方法是查看日志文件的结构。 如果
Po
键是第二个字符串,则可以执行以下操作:您可以使用以下方法包装它:
并遍历日志行:
map(dict_from_log, log_lines)
输出
相关问题 更多 >
编程相关推荐