[Python] How to consolidate multiple lines in a list using a common string?

Discussão em 'Python' iniciado por Stack, Outubro 6, 2024.

  1. Stack

    Stack Membro Participativo

    My office network csv sample file is as below. The file contains data of network interfaces, however the data is scattered across three lines in the list for any given interface (for example 'pe_node1_ge-1/2/2.20' got three lines of data).

    I am trying to consolidate these into a single line without changing the number of columns (0 to 15). Any help is greatly appreciated.

    lines_list:

    [Oct_04_09:19:08, PE_INT_DESC, pe_node1_ge-1/2/2.20, TO_PE3, up_up, , , , , , , , , , , WA_WE_PI_960_B_Pier

    Oct_04_09:19:09, PE_IPV4_ADDRESS, pe_node1_ge-1/2/2.20, , , , 192.168.20.2/24, 192.168.20.1, vrrp-group_1, , , , , , , WA_WE_PI_960_B_Pier

    Oct_04_09:19:18, PE_ROUTING_VRRP, pe_node1_ge-1/2/2.20, , , , , , , , master, 192.168.20.2, vr-uptime:14w2d11:30_mr-uptime:14w2d10:31__, VRRP, , WA_WE_PI_960_B_Pier

    Oct_04_09:19:19, PE_INT_DESC, pe_node2_ge-1/1/1.10, TO_PE2, up_up, , , , , , , , , , , WA_WE_PI_960_B_Pier

    Oct_04_09:19:20, PE_IPV4_ADDRESS, pe_node2_ge-1/1/1.10, , , , 192.168.10.2/24, 192.168.10.1, vrrp-group_1, , , , , , , WA_WE_PI_960_B_Pier

    Oct_04_09:19:28, PE_ROUTING_VRRP, pe_node2_ge-1/1/1.10, , , , , , , , master, 192.168.10.2, vr-uptime:13w1d11:30_mr-uptime:15w2d10:31__, VRRP, , WA_WE_PI_960_B_Pier]


    Expected List:

    [Oct_04_09:19:08, PE_INT, pe_node1_ge-1/2/2.20, TO_PE3, up_up, , 192.168.20.2/24, 192.168.20.1, vrrp-group_1, , master, 192.168.20.2, vr-uptime:14w2d11:30_mr-uptime:14w2d10:31__, VRRP, , WA_WE_PI_960_B_Pier

    Oct_04_09:19:19, PE_INT, pe_node1_ge-1/1/1.10, TO_PE2, up_up, , 192.168.10.2/24, 192.168.10.1, vrrp-group_1, , master, 192.168.10.2, vr-uptime:13w1d11:30_mr-uptime:15w2d10:31__, VRRP, , WA_WE_PI_960_B_Pier]

    Continue reading...

Compartilhe esta Página