更新时间:2023-01-23 14:07:46
您要做的第一件事是注释文件中的任何描述性数据.默认情况下,Networkx会将以#开头的任何行都视为注释.
The first thing you have to do is to comment any descriptive data in your file. By default, Networkx considers any line starting with # as a comment.
1 2 3 ...
1 2 3 ...
import networkx as net
FielName="GraphData.txt"
Graphtype=net.DiGraph() # use net.Graph() for undirected graph
# How to read from a file. Note: if your egde weights are int, change float to int.
G = net.read_edgelist(FielName, create_using=Graphtype, nodetype=int, data=(('weight',float),))
# Find the total number of degree, in_degree and out_degree for each node
for x in G.nodes():
print ("Node: ", x, " has total #degree: ",G.degree(x), " , In_degree: ", G.out_degree(x)," and out_degree: ", G.in_degree(x))
# Find the weight for each node
for u,v in G.edges():
print ("Weight of Edge ("+str(u)+","+str(v)+")", G.get_edge_data(u,v))
我建议您阅读在Networkx中读写图,并查看 read_edgelist
I recommend you to read the Reading and writing graphs in Networkx and have a look to read_edgelist