为什么我的 "networkx" 图不准确并且没有可视化我的输入数据?
Why my "networkx" plot is inaccurate and does not visualize my input data?
我正在尝试使用 Python 的“networkx”库可视化网络拓扑。但输出不准确,与预期不同。尤其是边缘没有显示,输出图没有组织,乱七八糟,不准确。
我的代码:
import networkx as nx
import matplotlib.pyplot as plt
import re
f = open("topology.conf", "r")
G = nx.Graph()
flag = 111
root = ''
for line in f:
for word in re.split(',|=| ', line):
if flag == 0:
root = word
flag = 111
if flag == 1:
G.add_edge(root, word)
flag = 111
if word == 'SwitchName':
flag = 0
continue
if word == 'Switches' or word == 'Nodes':
flag = 1
continue
nx.draw(G, with_labels=False)
plt.show()
f.close()
在与网络拓扑相关的输入数据中,“SwitchName”与“Switches”或“Nodes”中的每一项之间存在连接。
要可视化的输入数据:
SwitchName=rootSwitch Switches=island1,island2,island3,island4
SwitchName=island1 Switches=r235u10ibs,r235u11ibs,r235u12ibs,r235u13ibs,r235u14ibs,r235u15ibs,r235u16ibs,r235u1ibs,r235u2ibs,r235u3ibs,r235u4ibs,r235u5ibs,r235u6ibs,r235u7ibs,r235u8ibs,r235u9ibs
SwitchName=island2 Switches=r252u10ibs,r252u11ibs,r252u12ibs,r252u13ibs,r252u14ibs,r252u15ibs,r252u16ibs,r252u1ibs,r252u2ibs,r252u3ibs,r252u4ibs,r252u5ibs,r252u6ibs,r252u7ibs,r252u8ibs,r252u9ibs
SwitchName=island3 Switches=r235u19ibs,r235u20ibs,r235u21ibs,r235u22ibs,r235u23ibs,r235u24ibs,r235u25ibs,r235u26ibs,r235u27ibs,r235u28ibs,r235u29ibs,r235u30ibs,r235u31ibs,r235u32ibs,r235u33ibs,r235u34ibs
SwitchName=island4 Switches=r252u19ibs,r252u20ibs,r252u21ibs,r252u22ibs,r252u23ibs,r252u24ibs,r252u25ibs,r252u26ibs,r252u27ibs,r252u28ibs,r252u29ibs,r252u30ibs,r252u31ibs,r252u32ibs,r252u33ibs,r252u34ibs
SwitchName=r252u20ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r235u34ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u12ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u31ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u28ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u6ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u23ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u1ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r235u33ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u11ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r235u9ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u30ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u27ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u5ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u19ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u22ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u10ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u16ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u8ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u26ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u4ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u21ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r252u15ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r218u21ibl,r216u21ibl,r220u21ibl
SwitchName=r235u32ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u29ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u7ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u25ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u3ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u20ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r252u14ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u31ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u28ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u6ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r235u16ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u13ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u30ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u27ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r252u24ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u2ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r252u34ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u9ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r252u12ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u26ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u5ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u23ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u1ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u15ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u11ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u25ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u4ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u19ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u22ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r235u14ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u33ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r235u24ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r252u8ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u3ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u21ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r235u13ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u32ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u29ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u10ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r252u7ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u2ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r240u21ibl Nodes=r240n01,r240n02,r240n03,r240n04,r240n05,r240n06,r240n07,r240n08,r240n09,r240n10,r240n11,r240n12,r240n13,r240n14,r240n15,r240n16,r240n17,r240n18,r240n19,r240n20
SwitchName=r229u21ibl Nodes=r229n01,r229n02,r229n03,r229n04,r229n05,r229n06,r229n07,r229n08,r229n09,r229n10,r229n11,r229n12,r229n13,r229n14,r229n15,r229n16,r229n17,r229n18,r229n19,r229n20
SwitchName=r219u21ibl Nodes=r219n01,r219n02,r219n03,r219n04,r219n05,r219n06,r219n07,r219n08,r219n09,r219n10,r219n11,r219n12,r219n13,r219n14,r219n15,r219n16,r219n17,r219n18,r219n19,r219n20
SwitchName=r207u21ibl Nodes=r207n01,r207n02,r207n03,r207n04,r207n06,r207n05,r207n07,r207n08,r207n09,r207n10,r207n11,r207n12,r207n13,r207n14,r207n15,r207n16,r207n17,r207n18,r207n19,r207n20
SwitchName=r209u21ibl Nodes=r209n01,r209n02,r209n03,r209n04,r209n05,r209n06,r209n07,r209n08,r209n09,r209n10,r209n11,r209n12,r209n13,r209n14,r209n15,r209n16,r209n17,r209n18,r209n19,r209n20
SwitchName=r249u21ibl Nodes=r249n01,r249n02,r249n03,r249n04,r249n05,r249n06,r249n07,r249n08,r249n09,r249n10,r249n11,r249n12,r249n13,r249n14,r249n15,r249n16,r249n17,r249n18,r249n19,r249n20
SwitchName=r237u21ibl Nodes=r237n01,r237n02,r237n03,r237n04,r237n05,r237n06,r237n07,r237n08,r237n09,r237n10,r237n11,r237n12,r237n13,r237n14,r237n15,r237n16,r237n17,r237n18,r237n19,r237n20
SwitchName=r239u21ibl Nodes=r239n01,r239n02,r239n03,r239n04,r239n05,r239n07,r239n06,r239n08,r239n09,r239n10,r239n11,r239n12,r239n13,r239n14,r239n15,r239n16,r239n17,r239n18,r239n19,r239n20
SwitchName=r227u21ibl Nodes=r227n01,r227n02,r227n03,r227n04,r227n05,r227n06,r227n07,r227n08,r227n10,r227n11,r227n12,r227n13,r227n14,r227n16,r227n17,r227n18,r227n19,r227n20,r227n09,r227n15
SwitchName=r215u21ibl Nodes=r215n01,r215n02,r215n03,r215n04,r215n05,r215n06,r215n07,r215n08,r215n09,r215n10,r215n11,r215n12,r215n13,r215n14,r215n15,r215n16,r215n17,r215n18,r215n19,r215n20
SwitchName=r217u21ibl Nodes=r217n01,r217n02,r217n03,r217n04,r217n05,r217n06,r217n07,r217n08,r217n09,r217n10,r217n11,r217n12,r217n13,r217n14,r217n15,r217n16,r217n17,r217n18,r217n19,r217n20
SwitchName=r205u21ibl Nodes=r205n01,r205n02,r205n03,r205n04,r205n05,r205n06,r205n07,r205n08,r205n09,r205n10,r205n12,r205n11,r205n13,r205n14,r205n15,r205n16,r205n17,r205n18,r205n19,r205n20
SwitchName=r245u21ibl Nodes=r245n01,r245n02,r245n03,r245n04,r245n05,r245n06,r245n07,r245n08,r245n09,r245n10,r245n11,r245n12,r245n13,r245n14,r245n15,r245n16,r245n17,r245n18,r245n19,r245n20
SwitchName=r247u21ibl Nodes=r247n01,r247n02,r247n03,r247n04,r247n05,r247n06,r247n07,r247n08,r247n09,r247n10,r247n11,r247n12,r247n13,r247n14,r247n15,r247n16,r247n17,r247n18,r247n19,r247n20
SwitchName=r223u21ibl Nodes=r223n01,r223n02,r223n03,r223n04,r223n05,r223n06,r223n07,r223n08,r223n09,r223n10,r223n11,r223n12,r223n13,r223n14,r223n15,r223n16,r223n17,r223n18,r223n19,r223n20
SwitchName=r225u21ibl Nodes=r225n01,r225n02,r225n04,r225n03,r225n05,r225n06,r225n07,r225n08,r225n09,r225n10,r225n11,r225n12,r225n13,r225n14,r225n15,r225n16,r225n17,r225n18,r225n19,r225n20
SwitchName=r213u21ibl Nodes=r213n01,r213n02,r213n03,r213n04,r213n05,r213n06,r213n07,r213n08,r213n09,r213n10,r213n11,r213n12,r213n13,r213n14,r213n15,r213n16,r213n17,r213n18,r213n19,r213n20
SwitchName=r253u21ibl Nodes=r253n01,r253n02,r253n03,r253n04,r253n05,r253n06,r253n07,r253n08,r253n09,r253n10,r253n11,r253n12,r253n13,r253n14,r253n15,r253n16,r253n17,r253n18,r253n19,r253n20
SwitchName=r255u21ibl Nodes=r255n01,r255n02,r255n03,r255n04,r255n05,r255n06,r255n07,r255n08,r255n09,r255n10,r255n11,r255n12,r255n13,r255n14,r255n15,r255n16,r255n17,r255n18,r255n19,r255n20
SwitchName=r243u21ibl Nodes=r243n01,r243n02,r243n03,r243n04,r243n05,r243n06,r243n07,r243n08,r243n09,r243n10,r243n11,r243n12,r243n13,r243n14,r243n15,r243n16,r243n17,r243n18,r243n19,r243n20
SwitchName=r231u21ibl Nodes=r231n01,r231n02,r231n03,r231n04,r231n05,r231n06,r231n07,r231n08,r231n09,r231n10,r231n11,r231n12,r231n13,r231n14,r231n15,r231n16,r231n17,r231n18,r231n19,r231n20
SwitchName=r233u21ibl Nodes=r233n01,r233n02,r233n03,r233n04,r233n05,r233n06,r233n07,r233n08,r233n09,r233n10,r233n11,r233n12,r233n13,r233n14,r233n15,r233n16,r233n17,r233n18,r233n19,r233n20
SwitchName=r221u21ibl Nodes=r221n01,r221n02,r221n03,r221n04,r221n05,r221n06,r221n07,r221n08,r221n09,r221n10,r221n11,r221n12,r221n13,r221n14,r221n15,r221n16,r221n17,r221n18,r221n19,r221n20
SwitchName=r211u21ibl Nodes=r211n01,r211n02,r211n03,r211n04,r211n05,r211n06,r211n07,r211n08,r211n09,r211n10,r211n11,r211n12,r211n13,r211n14,r211n15,r211n16,r211n17,r211n18,r211n19,r211n20
SwitchName=r251u21ibl Nodes=r251n01,r251n02,r251n03,r251n04,r251n05,r251n06,r251n07,r251n08,r251n09,r251n10,r251n11,r251n12,r251n13,r251n14,r251n15,r251n16,r251n17,r251n18,r251n19,r251n20
SwitchName=r241u21ibl Nodes=r241n01,r241n02,r241n03,r241n04,r241n05,r241n06,r241n07,r241n08,r241n09,r241n10,r241n11,r241n12,r241n13,r241n14,r241n15,r241n16,r241n17,r241n18,r241n19,r241n20
SwitchName=r208u21ibl Nodes=r208n01,r208n02,r208n03,r208n04,r208n05,r208n06,r208n07,r208n08,r208n09,r208n10,r208n11,r208n12,r208n13,r208n14,r208n15,r208n16,r208n17,r208n18,r208n19,r208n20
SwitchName=r238u21ibl Nodes=r238n01,r238n02,r238n03,r238n04,r238n05,r238n06,r238n07,r238n08,r238n09,r238n10,r238n11,r238n12,r238n13,r238n14,r238n15,r238n16,r238n17,r238n18,r238n19,r238n20
SwitchName=r228u21ibl Nodes=r228n01,r228n03,r228n02,r228n04,r228n05,r228n06,r228n07,r228n09,r228n10,r228n08,r228n11,r228n12,r228n13,r228n14,r228n15,r228n16,r228n17,r228n18,r228n19,r228n20
SwitchName=r216u21ibl Nodes=r216n01,r216n02,r216n03,r216n04,r216n05,r216n06,r216n07,r216n08,r216n09,r216n10,r216n11,r216n12,r216n13,r216n14,r216n15,r216n16,r216n17,r216n18,r216n19,r216n20
SwitchName=r218u21ibl Nodes=r218n01,r218n02,r218n03,r218n04,r218n05,r218n06,r218n07,r218n08,r218n09,r218n10,r218n11,r218n12,r218n13,r218n14,r218n15,r218n16,r218n17,r218n18,r218n19,r218n20
SwitchName=r206u21ibl Nodes=r206n01,r206n02,r206n03,r206n04,r206n05,r206n06,r206n07,r206n08,r206n09,r206n10,r206n11,r206n12,r206n13,r206n14,r206n15,r206n16,r206n17,r206n18,r206n19,r206n20
SwitchName=r246u21ibl Nodes=r246n01,r246n02,r246n03,r246n04,r246n05,r246n06,r246n07,r246n08,r246n09,r246n10,r246n11,r246n12,r246n13,r246n14,r246n15,r246n16,r246n17,r246n18,r246n19,r246n20
SwitchName=r248u21ibl Nodes=r248n01,r248n02,r248n03,r248n04,r248n05,r248n06,r248n07,r248n08,r248n09,r248n10,r248n11,r248n12,r248n13,r248n14,r248n15,r248n16,r248n17,r248n18,r248n19,r248n20
SwitchName=r236u21ibl Nodes=r236n01,r236n02,r236n03,r236n04,r236n05,r236n06,r236n07,r236n08,r236n09,r236n10,r236n11,r236n12,r236n13,r236n14,r236n15,r236n16,r236n17,r236n18,r236n19,r236n20
SwitchName=r224u21ibl Nodes=r224n01,r224n02,r224n03,r224n04,r224n05,r224n06,r224n07,r224n08,r224n09,r224n10,r224n11,r224n12,r224n13,r224n14,r224n15,r224n16,r224n17,r224n18,r224n19,r224n20
SwitchName=r226u21ibl Nodes=r226n01,r226n02,r226n03,r226n04,r226n05,r226n06,r226n08,r226n07,r226n09,r226n10,r226n11,r226n12,r226n13,r226n14,r226n15,r226n16,r226n17,r226n18,r226n19,r226n20
SwitchName=r214u21ibl Nodes=r214n01,r214n02,r214n03,r214n04,r214n05,r214n06,r214n07,r214n08,r214n09,r214n10,r214n11,r214n12,r214n14,r214n13,r214n15,r214n16,r214n17,r214n19,r214n18,r214n20
SwitchName=r256u21ibl Nodes=r256n01,r256n02,r256n03,r256n04,r256n05,r256n06,r256n07,r256n08,r256n09,r256n10,r256n11,r256n12,r256n13,r256n14,r256n15,r256n16,r256n17,r256n18,r256n19,r256n20
SwitchName=r244u21ibl Nodes=r244n01,r244n02,r244n03,r244n04,r244n05,r244n06,r244n08,r244n07,r244n09,r244n10,r244n11,r244n12,r244n13,r244n14,r244n15,r244n16,r244n17,r244n18,r244n19,r244n20
SwitchName=r232u21ibl Nodes=r232n01,r232n02,r232n03,r232n04,r232n05,r232n06,r232n07,r232n08,r232n09,r232n10,r232n11,r232n12,r232n13,r232n14,r232n15,r232n16,r232n17,r232n18,r232n19,r232n20
SwitchName=r234u21ibl Nodes=r234n01,r234n02,r234n03,r234n04,r234n05,r234n06,r234n07,r234n08,r234n09,r234n10,r234n11,r234n12,r234n13,r234n14,r234n15,r234n16,r234n17,r234n18,r234n19,r234n20
SwitchName=r222u21ibl Nodes=r222n01,r222n02,r222n03,r222n04,r222n05,r222n06,r222n07,r222n08,r222n09,r222n10,r222n11,r222n12,r222n13,r222n14,r222n15,r222n16,r222n17,r222n18,r222n19,r222n20
SwitchName=r210u21ibl Nodes=r210n01,r210n02,r210n03,r210n04,r210n05,r210n06,r210n07,r210n08,r210n10,r210n09,r210n11,r210n12,r210n13,r210n14,r210n15,r210n16,r210n17,r210n18,r210n19,r210n20
SwitchName=r212u21ibl Nodes=r212n01,r212n02,r212n03,r212n04,r212n05,r212n06,r212n11,r212n12,r212n09,r212n10,r212n07,r212n08,r212n13,r212n14,r212n15,r212n16,r212n17,r212n18,r212n19,r212n20
SwitchName=r254u21ibl Nodes=r254n01,r254n02,r254n03,r254n04,r254n05,r254n06,r254n07,r254n08,r254n09,r254n10,r254n11,r254n12,r254n13,r254n14,r254n15,r254n16,r254n17,r254n18,r254n19,r254n20
SwitchName=r242u21ibl Nodes=r242n01,r242n02,r242n03,r242n04,r242n05,r242n06,r242n07,r242n08,r242n09,r242n10,r242n11,r242n12,r242n13,r242n14,r242n15,r242n16,r242n18,r242n17,r242n19,r242n20
SwitchName=r220u21ibl Nodes=r220n01,r220n02,r220n03,r220n04,r220n05,r220n06,r220n07,r220n08,r220n09,r220n10,r220n11,r220n12,r220n13,r220n14,r220n15,r220n16,r220n17,r220n19,r220n18,r220n20
SwitchName=r250u21ibl Nodes=r250n01,r250n02,r250n04,r250n03,r250n05,r250n06,r250n07,r250n08,r250n09,r250n10,r250n11,r250n12,r250n13,r250n14,r250n15,r250n16,r250n18,r250n17,r250n19,r250n20
最后,输出文件应该是这样的(不完全一样,但格式类似)
预期输出
我认为问题出在您的解析中。您可以通过在代码中打印所有边缘(通过添加行 [e for e in G.edges]
)来检查它。这是解决问题的示例:
import networkx as nx
import matplotlib.pyplot as plt
import re
f = open("topology.conf", "r")
G = nx.Graph()
for line in f:
seg = re.split(' ', line)
sources = re.split('=', seg[0])
source = sources[1]
for word in re.split(',|=| ', seg[1]):
if word == 'Switches' or word == 'Nodes':
continue
G.add_edge(source, word.strip())
nx.draw(G, with_labels=False)
plt.show()
f.close()
# [e for e in G.edges]
这是示例输出:
我正在尝试使用 Python 的“networkx”库可视化网络拓扑。但输出不准确,与预期不同。尤其是边缘没有显示,输出图没有组织,乱七八糟,不准确。
我的代码:
import networkx as nx
import matplotlib.pyplot as plt
import re
f = open("topology.conf", "r")
G = nx.Graph()
flag = 111
root = ''
for line in f:
for word in re.split(',|=| ', line):
if flag == 0:
root = word
flag = 111
if flag == 1:
G.add_edge(root, word)
flag = 111
if word == 'SwitchName':
flag = 0
continue
if word == 'Switches' or word == 'Nodes':
flag = 1
continue
nx.draw(G, with_labels=False)
plt.show()
f.close()
在与网络拓扑相关的输入数据中,“SwitchName”与“Switches”或“Nodes”中的每一项之间存在连接。
要可视化的输入数据:
SwitchName=rootSwitch Switches=island1,island2,island3,island4
SwitchName=island1 Switches=r235u10ibs,r235u11ibs,r235u12ibs,r235u13ibs,r235u14ibs,r235u15ibs,r235u16ibs,r235u1ibs,r235u2ibs,r235u3ibs,r235u4ibs,r235u5ibs,r235u6ibs,r235u7ibs,r235u8ibs,r235u9ibs
SwitchName=island2 Switches=r252u10ibs,r252u11ibs,r252u12ibs,r252u13ibs,r252u14ibs,r252u15ibs,r252u16ibs,r252u1ibs,r252u2ibs,r252u3ibs,r252u4ibs,r252u5ibs,r252u6ibs,r252u7ibs,r252u8ibs,r252u9ibs
SwitchName=island3 Switches=r235u19ibs,r235u20ibs,r235u21ibs,r235u22ibs,r235u23ibs,r235u24ibs,r235u25ibs,r235u26ibs,r235u27ibs,r235u28ibs,r235u29ibs,r235u30ibs,r235u31ibs,r235u32ibs,r235u33ibs,r235u34ibs
SwitchName=island4 Switches=r252u19ibs,r252u20ibs,r252u21ibs,r252u22ibs,r252u23ibs,r252u24ibs,r252u25ibs,r252u26ibs,r252u27ibs,r252u28ibs,r252u29ibs,r252u30ibs,r252u31ibs,r252u32ibs,r252u33ibs,r252u34ibs
SwitchName=r252u20ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r235u34ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u12ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u31ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u28ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u6ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u23ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u1ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r235u33ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u11ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r235u9ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u30ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u27ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u5ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u19ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u22ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u10ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u16ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u8ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u26ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u4ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u21ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r252u15ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r218u21ibl,r216u21ibl,r220u21ibl
SwitchName=r235u32ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u29ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u7ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u25ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u3ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u20ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r252u14ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u31ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u28ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u6ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r235u16ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u13ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u30ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u27ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r252u24ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u2ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r252u34ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u9ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r252u12ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u26ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u5ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u23ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u1ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u15ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u11ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u25ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r235u4ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u19ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u22ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r235u14ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u33ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r235u24ibs Switches=r229u21ibl,r231u21ibl,r232u21ibl,r223u21ibl,r225u21ibl,r224u21ibl,r226u21ibl,r236u21ibl,r227u21ibl,r237u21ibl,r239u21ibl,r228u21ibl,r238u21ibl
SwitchName=r252u8ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u3ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u21ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r235u13ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r252u32ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u29ibs Switches=r243u21ibl,r253u21ibl,r255u21ibl,r244u21ibl,r256u21ibl,r245u21ibl,r247u21ibl,r246u21ibl,r248u21ibl,r240u21ibl,r241u21ibl,r242u21ibl,r254u21ibl
SwitchName=r252u10ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r252u7ibs Switches=r219u21ibl,r221u21ibl,r222u21ibl,r213u21ibl,r214u21ibl,r250u21ibl,r215u21ibl,r217u21ibl,r249u21ibl,r251u21ibl,r216u21ibl,r218u21ibl,r220u21ibl
SwitchName=r235u2ibs Switches=r207u21ibl,r209u21ibl,r211u21ibl,r233u21ibl,r208u21ibl,r210u21ibl,r212u21ibl,r234u21ibl,r205u21ibl,r206u21ibl
SwitchName=r240u21ibl Nodes=r240n01,r240n02,r240n03,r240n04,r240n05,r240n06,r240n07,r240n08,r240n09,r240n10,r240n11,r240n12,r240n13,r240n14,r240n15,r240n16,r240n17,r240n18,r240n19,r240n20
SwitchName=r229u21ibl Nodes=r229n01,r229n02,r229n03,r229n04,r229n05,r229n06,r229n07,r229n08,r229n09,r229n10,r229n11,r229n12,r229n13,r229n14,r229n15,r229n16,r229n17,r229n18,r229n19,r229n20
SwitchName=r219u21ibl Nodes=r219n01,r219n02,r219n03,r219n04,r219n05,r219n06,r219n07,r219n08,r219n09,r219n10,r219n11,r219n12,r219n13,r219n14,r219n15,r219n16,r219n17,r219n18,r219n19,r219n20
SwitchName=r207u21ibl Nodes=r207n01,r207n02,r207n03,r207n04,r207n06,r207n05,r207n07,r207n08,r207n09,r207n10,r207n11,r207n12,r207n13,r207n14,r207n15,r207n16,r207n17,r207n18,r207n19,r207n20
SwitchName=r209u21ibl Nodes=r209n01,r209n02,r209n03,r209n04,r209n05,r209n06,r209n07,r209n08,r209n09,r209n10,r209n11,r209n12,r209n13,r209n14,r209n15,r209n16,r209n17,r209n18,r209n19,r209n20
SwitchName=r249u21ibl Nodes=r249n01,r249n02,r249n03,r249n04,r249n05,r249n06,r249n07,r249n08,r249n09,r249n10,r249n11,r249n12,r249n13,r249n14,r249n15,r249n16,r249n17,r249n18,r249n19,r249n20
SwitchName=r237u21ibl Nodes=r237n01,r237n02,r237n03,r237n04,r237n05,r237n06,r237n07,r237n08,r237n09,r237n10,r237n11,r237n12,r237n13,r237n14,r237n15,r237n16,r237n17,r237n18,r237n19,r237n20
SwitchName=r239u21ibl Nodes=r239n01,r239n02,r239n03,r239n04,r239n05,r239n07,r239n06,r239n08,r239n09,r239n10,r239n11,r239n12,r239n13,r239n14,r239n15,r239n16,r239n17,r239n18,r239n19,r239n20
SwitchName=r227u21ibl Nodes=r227n01,r227n02,r227n03,r227n04,r227n05,r227n06,r227n07,r227n08,r227n10,r227n11,r227n12,r227n13,r227n14,r227n16,r227n17,r227n18,r227n19,r227n20,r227n09,r227n15
SwitchName=r215u21ibl Nodes=r215n01,r215n02,r215n03,r215n04,r215n05,r215n06,r215n07,r215n08,r215n09,r215n10,r215n11,r215n12,r215n13,r215n14,r215n15,r215n16,r215n17,r215n18,r215n19,r215n20
SwitchName=r217u21ibl Nodes=r217n01,r217n02,r217n03,r217n04,r217n05,r217n06,r217n07,r217n08,r217n09,r217n10,r217n11,r217n12,r217n13,r217n14,r217n15,r217n16,r217n17,r217n18,r217n19,r217n20
SwitchName=r205u21ibl Nodes=r205n01,r205n02,r205n03,r205n04,r205n05,r205n06,r205n07,r205n08,r205n09,r205n10,r205n12,r205n11,r205n13,r205n14,r205n15,r205n16,r205n17,r205n18,r205n19,r205n20
SwitchName=r245u21ibl Nodes=r245n01,r245n02,r245n03,r245n04,r245n05,r245n06,r245n07,r245n08,r245n09,r245n10,r245n11,r245n12,r245n13,r245n14,r245n15,r245n16,r245n17,r245n18,r245n19,r245n20
SwitchName=r247u21ibl Nodes=r247n01,r247n02,r247n03,r247n04,r247n05,r247n06,r247n07,r247n08,r247n09,r247n10,r247n11,r247n12,r247n13,r247n14,r247n15,r247n16,r247n17,r247n18,r247n19,r247n20
SwitchName=r223u21ibl Nodes=r223n01,r223n02,r223n03,r223n04,r223n05,r223n06,r223n07,r223n08,r223n09,r223n10,r223n11,r223n12,r223n13,r223n14,r223n15,r223n16,r223n17,r223n18,r223n19,r223n20
SwitchName=r225u21ibl Nodes=r225n01,r225n02,r225n04,r225n03,r225n05,r225n06,r225n07,r225n08,r225n09,r225n10,r225n11,r225n12,r225n13,r225n14,r225n15,r225n16,r225n17,r225n18,r225n19,r225n20
SwitchName=r213u21ibl Nodes=r213n01,r213n02,r213n03,r213n04,r213n05,r213n06,r213n07,r213n08,r213n09,r213n10,r213n11,r213n12,r213n13,r213n14,r213n15,r213n16,r213n17,r213n18,r213n19,r213n20
SwitchName=r253u21ibl Nodes=r253n01,r253n02,r253n03,r253n04,r253n05,r253n06,r253n07,r253n08,r253n09,r253n10,r253n11,r253n12,r253n13,r253n14,r253n15,r253n16,r253n17,r253n18,r253n19,r253n20
SwitchName=r255u21ibl Nodes=r255n01,r255n02,r255n03,r255n04,r255n05,r255n06,r255n07,r255n08,r255n09,r255n10,r255n11,r255n12,r255n13,r255n14,r255n15,r255n16,r255n17,r255n18,r255n19,r255n20
SwitchName=r243u21ibl Nodes=r243n01,r243n02,r243n03,r243n04,r243n05,r243n06,r243n07,r243n08,r243n09,r243n10,r243n11,r243n12,r243n13,r243n14,r243n15,r243n16,r243n17,r243n18,r243n19,r243n20
SwitchName=r231u21ibl Nodes=r231n01,r231n02,r231n03,r231n04,r231n05,r231n06,r231n07,r231n08,r231n09,r231n10,r231n11,r231n12,r231n13,r231n14,r231n15,r231n16,r231n17,r231n18,r231n19,r231n20
SwitchName=r233u21ibl Nodes=r233n01,r233n02,r233n03,r233n04,r233n05,r233n06,r233n07,r233n08,r233n09,r233n10,r233n11,r233n12,r233n13,r233n14,r233n15,r233n16,r233n17,r233n18,r233n19,r233n20
SwitchName=r221u21ibl Nodes=r221n01,r221n02,r221n03,r221n04,r221n05,r221n06,r221n07,r221n08,r221n09,r221n10,r221n11,r221n12,r221n13,r221n14,r221n15,r221n16,r221n17,r221n18,r221n19,r221n20
SwitchName=r211u21ibl Nodes=r211n01,r211n02,r211n03,r211n04,r211n05,r211n06,r211n07,r211n08,r211n09,r211n10,r211n11,r211n12,r211n13,r211n14,r211n15,r211n16,r211n17,r211n18,r211n19,r211n20
SwitchName=r251u21ibl Nodes=r251n01,r251n02,r251n03,r251n04,r251n05,r251n06,r251n07,r251n08,r251n09,r251n10,r251n11,r251n12,r251n13,r251n14,r251n15,r251n16,r251n17,r251n18,r251n19,r251n20
SwitchName=r241u21ibl Nodes=r241n01,r241n02,r241n03,r241n04,r241n05,r241n06,r241n07,r241n08,r241n09,r241n10,r241n11,r241n12,r241n13,r241n14,r241n15,r241n16,r241n17,r241n18,r241n19,r241n20
SwitchName=r208u21ibl Nodes=r208n01,r208n02,r208n03,r208n04,r208n05,r208n06,r208n07,r208n08,r208n09,r208n10,r208n11,r208n12,r208n13,r208n14,r208n15,r208n16,r208n17,r208n18,r208n19,r208n20
SwitchName=r238u21ibl Nodes=r238n01,r238n02,r238n03,r238n04,r238n05,r238n06,r238n07,r238n08,r238n09,r238n10,r238n11,r238n12,r238n13,r238n14,r238n15,r238n16,r238n17,r238n18,r238n19,r238n20
SwitchName=r228u21ibl Nodes=r228n01,r228n03,r228n02,r228n04,r228n05,r228n06,r228n07,r228n09,r228n10,r228n08,r228n11,r228n12,r228n13,r228n14,r228n15,r228n16,r228n17,r228n18,r228n19,r228n20
SwitchName=r216u21ibl Nodes=r216n01,r216n02,r216n03,r216n04,r216n05,r216n06,r216n07,r216n08,r216n09,r216n10,r216n11,r216n12,r216n13,r216n14,r216n15,r216n16,r216n17,r216n18,r216n19,r216n20
SwitchName=r218u21ibl Nodes=r218n01,r218n02,r218n03,r218n04,r218n05,r218n06,r218n07,r218n08,r218n09,r218n10,r218n11,r218n12,r218n13,r218n14,r218n15,r218n16,r218n17,r218n18,r218n19,r218n20
SwitchName=r206u21ibl Nodes=r206n01,r206n02,r206n03,r206n04,r206n05,r206n06,r206n07,r206n08,r206n09,r206n10,r206n11,r206n12,r206n13,r206n14,r206n15,r206n16,r206n17,r206n18,r206n19,r206n20
SwitchName=r246u21ibl Nodes=r246n01,r246n02,r246n03,r246n04,r246n05,r246n06,r246n07,r246n08,r246n09,r246n10,r246n11,r246n12,r246n13,r246n14,r246n15,r246n16,r246n17,r246n18,r246n19,r246n20
SwitchName=r248u21ibl Nodes=r248n01,r248n02,r248n03,r248n04,r248n05,r248n06,r248n07,r248n08,r248n09,r248n10,r248n11,r248n12,r248n13,r248n14,r248n15,r248n16,r248n17,r248n18,r248n19,r248n20
SwitchName=r236u21ibl Nodes=r236n01,r236n02,r236n03,r236n04,r236n05,r236n06,r236n07,r236n08,r236n09,r236n10,r236n11,r236n12,r236n13,r236n14,r236n15,r236n16,r236n17,r236n18,r236n19,r236n20
SwitchName=r224u21ibl Nodes=r224n01,r224n02,r224n03,r224n04,r224n05,r224n06,r224n07,r224n08,r224n09,r224n10,r224n11,r224n12,r224n13,r224n14,r224n15,r224n16,r224n17,r224n18,r224n19,r224n20
SwitchName=r226u21ibl Nodes=r226n01,r226n02,r226n03,r226n04,r226n05,r226n06,r226n08,r226n07,r226n09,r226n10,r226n11,r226n12,r226n13,r226n14,r226n15,r226n16,r226n17,r226n18,r226n19,r226n20
SwitchName=r214u21ibl Nodes=r214n01,r214n02,r214n03,r214n04,r214n05,r214n06,r214n07,r214n08,r214n09,r214n10,r214n11,r214n12,r214n14,r214n13,r214n15,r214n16,r214n17,r214n19,r214n18,r214n20
SwitchName=r256u21ibl Nodes=r256n01,r256n02,r256n03,r256n04,r256n05,r256n06,r256n07,r256n08,r256n09,r256n10,r256n11,r256n12,r256n13,r256n14,r256n15,r256n16,r256n17,r256n18,r256n19,r256n20
SwitchName=r244u21ibl Nodes=r244n01,r244n02,r244n03,r244n04,r244n05,r244n06,r244n08,r244n07,r244n09,r244n10,r244n11,r244n12,r244n13,r244n14,r244n15,r244n16,r244n17,r244n18,r244n19,r244n20
SwitchName=r232u21ibl Nodes=r232n01,r232n02,r232n03,r232n04,r232n05,r232n06,r232n07,r232n08,r232n09,r232n10,r232n11,r232n12,r232n13,r232n14,r232n15,r232n16,r232n17,r232n18,r232n19,r232n20
SwitchName=r234u21ibl Nodes=r234n01,r234n02,r234n03,r234n04,r234n05,r234n06,r234n07,r234n08,r234n09,r234n10,r234n11,r234n12,r234n13,r234n14,r234n15,r234n16,r234n17,r234n18,r234n19,r234n20
SwitchName=r222u21ibl Nodes=r222n01,r222n02,r222n03,r222n04,r222n05,r222n06,r222n07,r222n08,r222n09,r222n10,r222n11,r222n12,r222n13,r222n14,r222n15,r222n16,r222n17,r222n18,r222n19,r222n20
SwitchName=r210u21ibl Nodes=r210n01,r210n02,r210n03,r210n04,r210n05,r210n06,r210n07,r210n08,r210n10,r210n09,r210n11,r210n12,r210n13,r210n14,r210n15,r210n16,r210n17,r210n18,r210n19,r210n20
SwitchName=r212u21ibl Nodes=r212n01,r212n02,r212n03,r212n04,r212n05,r212n06,r212n11,r212n12,r212n09,r212n10,r212n07,r212n08,r212n13,r212n14,r212n15,r212n16,r212n17,r212n18,r212n19,r212n20
SwitchName=r254u21ibl Nodes=r254n01,r254n02,r254n03,r254n04,r254n05,r254n06,r254n07,r254n08,r254n09,r254n10,r254n11,r254n12,r254n13,r254n14,r254n15,r254n16,r254n17,r254n18,r254n19,r254n20
SwitchName=r242u21ibl Nodes=r242n01,r242n02,r242n03,r242n04,r242n05,r242n06,r242n07,r242n08,r242n09,r242n10,r242n11,r242n12,r242n13,r242n14,r242n15,r242n16,r242n18,r242n17,r242n19,r242n20
SwitchName=r220u21ibl Nodes=r220n01,r220n02,r220n03,r220n04,r220n05,r220n06,r220n07,r220n08,r220n09,r220n10,r220n11,r220n12,r220n13,r220n14,r220n15,r220n16,r220n17,r220n19,r220n18,r220n20
SwitchName=r250u21ibl Nodes=r250n01,r250n02,r250n04,r250n03,r250n05,r250n06,r250n07,r250n08,r250n09,r250n10,r250n11,r250n12,r250n13,r250n14,r250n15,r250n16,r250n18,r250n17,r250n19,r250n20
最后,输出文件应该是这样的(不完全一样,但格式类似)
预期输出
我认为问题出在您的解析中。您可以通过在代码中打印所有边缘(通过添加行 [e for e in G.edges]
)来检查它。这是解决问题的示例:
import networkx as nx
import matplotlib.pyplot as plt
import re
f = open("topology.conf", "r")
G = nx.Graph()
for line in f:
seg = re.split(' ', line)
sources = re.split('=', seg[0])
source = sources[1]
for word in re.split(',|=| ', seg[1]):
if word == 'Switches' or word == 'Nodes':
continue
G.add_edge(source, word.strip())
nx.draw(G, with_labels=False)
plt.show()
f.close()
# [e for e in G.edges]
这是示例输出: