如何使用公共字符串合并列表中的多行?

问题描述 投票:0回答:1

我的办公室网络csv示例文件如下。该文件包含网络接口的数据,但是对于任何给定接口,数据分散在列表中的三行中(例如“pe_node1_ge-1/2/2.20”有三行数据)。

我试图将它们合并到一行中,而不更改列数(0 到 15)。非常感谢任何帮助。

行_列表:

[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]

预期清单:

[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]
python list
1个回答
0
投票

“不改变列数(0到15)” 您的文件中只有 15 列,0 到 15 表示 16 列,所以我假设您的意思是 15 列。

导入 CSV,并使用 Pandas 分组,这是 stackoverflow.com 上多次介绍的主题,或在文档中进行了解释。

import pandas

cols = ['col0','col1','col2','col3','col4','col5','col6','col7','col8','col9','col10','col11','col12','col13','col14']
x =pandas.read_csv('lines_list', sep=',', names=cols)

#print(x)
print(x.groupby('col1').max())
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.