【问题标题】:log file to convert into dictionary format using python使用python将日志文件转换为字典格式
【发布时间】: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


【解决方案1】:

嗯,直接的方法是查看日志文件的结构。 如果Po 键是第二个字符串,可以这样做:

log_line = '1     Po1(SU)     Eth      LACP      Eth1/24(P)   Eth2/24(P)   Eth3/24/10(P)'

log_line_list = log_line.split()   # will look like: ['1', 'Po1(SU)', 'Eth', ...]
k = log_line_list[1]               # will hold 'Po1(SU)'
log_line_list.remove(k)            # remove the key from the list

d = {k: log_line_list}

print(d)

>> {'Po1(SU)': ['1', 'Eth', 'LACP', 'Eth1/24(P)', 'Eth2/24(P)', 'Eth3/24/10(P)']}

你可以用一个方法来包装它:

def dict_from_log(log_line):
    l = log_line.split()
    k = l[1]
    l.remove(k)
    return {k: l}

并遍历您的日志行:map(dict_from_log, log_lines)

【讨论】:

    【解决方案2】:
    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)
        '''
    
    l = [i.strip() for i in show_pcs.split('\n') if len(i.strip())>0]
    
    l = [j for i in l for j in i.split() if len(j)>0] 
    
    lst1 = []
    lst = []
    for i in l:
        if i.isdigit():
            if lst !=[]:
                lst1.append(lst)
            else:
                lst=[i]
        else:
            lst.append(i)
    
    
    dic = {i[1]:i[2:] for i in lst1}
    
    print(dic)
    

    输出

    {'Po1(SU)': ['Eth', 'LACP', 'Eth1/24(P)', 'Eth2/24(P)', 'Eth3/24/10(P)', 'Eth4/24(P)', '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)', 'Po3(SD)', 'Eth', 'NONE', '--', 'Po4(SD)', 'Eth', 'NONE', '--', '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)', '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)', 'Po7(SD)', 'Eth', 'LACP', 'Eth1/22(P)', 'Eth2/22(P)', 'Eth3/22(P)', 'Eth107/1/22(D)', 'Po8(SU)', 'Eth', 'LACP', 'Eth1/23(P)', 'Eth2/23(P)', 'Eth3/23(P)']}
    

    【讨论】:

    • 他还要求列表中的行号,例如'1'、'2'等。
    猜你喜欢
    • 2021-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2019-10-28
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多