【发布时间】:2019-10-18 08:06:28
【问题描述】:
我需要使用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': ....}
【问题讨论】:
-
预期输出 --- { 'Po1' : ['Eth1/24(P)', 'Eth2/24(P)', 'Eth3/24/10(P)','Eth4 /24(P)'], 'Po2': ....}
-
因为你的日志没有正确的方式,因为一些日志进入一个新行,你也不能在这里使用正则表达式。你需要自己写代码
标签: python python-3.x list