多的不说,看代码就懂了:
import networkx as nx
import matplotlib.pyplot as plt
textline = '1 2 3'
fh = open('test.edgelist','w')
d = fh.write(textline)
fh.close()
G = nx.read_edgelist('test.edgelist', create_using=nx.DiGraph(),nodetype=int, data=(('weight',float),))
nx.draw(G)
plt.show()
结果如下:
有点奇葩的是,
G = nx.read_edgelist('test.edgelist', create_using=nx.DiGraph(),nodetype=int, data=(('weight',float),))
里
data=(('weight',float),
要放在最后面,而且要多加个逗号,不然出错。不懂