日志文件以使用python转换为字典格式

时间:2019-10-18 08:06:28

标签: python python-3.x list

我需要使用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': ....}

2 个答案:

答案 0 :(得分:0)

嗯,直接的方法是查看日志文件的结构。 如果<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <div class="form-group row"> <label class="col-sm-2 col-form-label">Name<i class="fa fa-asterisk required-span" aria-hidden="true"></i></label> <div class="col-sm-4"> <input class="form-control" placeholder="Name" name="Name" required> </div> <div class="col-md-2"></div> <div class="col-sm-4 toggleDiv"> <div style="width: 100% ; height: 250px; background-color: black;"> </div> </div> </div> <div class="form-group row"> <label class="col-sm-2 col-form-label">Name <i class="fa fa-asterisk required-span" aria-hidden="true"></i></label> <div class="col-sm-4"> <input #Name class="form-control" placeholder="Name" name="Name" required> </div> </div> <div class="form-group row"> <label class="col-sm-2 col-form-label">Name <i class="fa fa-asterisk required-span" aria-hidden="true"></i></label> <div class="col-sm-4"> <input #Name class="form-control" placeholder="Name" name="Name" required> </div> </div> <div class="form-group row"> <label class="col-sm-2 col-form-label">Name <i class="fa fa-asterisk required-span" aria-hidden="true"></i></label> <div class="col-sm-4"> <input #Name class="form-control" placeholder="Name" name="Name" required> </div> </div> <div class="form-group row"> <label class="col-sm-2 col-form-label">Name <i class="fa fa-asterisk required-span" aria-hidden="true"></i></label> <div class="col-sm-4"> <input type="Name" #Name class="form-control" placeholder="Name" name="Name" required> </div> <label class="col-sm-2 col-form-label">Name <i class="fa fa-asterisk required-span" aria-hidden="true"></i></label> <div class="col-sm-4"> <input type="Name" #Name class="form-control" placeholder="Name" name="Name" required> </div> </div>键是第二个字符串,则可以执行以下操作:

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}

答案 1 :(得分:0)

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)']}