Home > other >  How to use the networkx read TXT in figure data?
How to use the networkx read TXT in figure data?

Time:11-26

Requirements:
How to "train". TXT said in the figure, read networkx object?

# "train". TXT (with the interpretation of the data set below)
T # 11808
V 1 C
V 2 C
3 O v
V 4 O
V 5 C
V 6 S
V 7 S
8 O v
9 O v
10 C v
E 1 2 1
E 4 1
E 5 1
E 2, 3, 1
E 2 10 1
E 5 6 1
E 6 July 1
E 7 8 2
E 7 9 2
E 7 10 1
T # 15905
V 1 C
V 2 C
V 3 C
V 4 C
V 5 C
V 6 C
V 7 S
V 8 C
9 v C
10 C v
V 11 C
V 12 C
V 13 N
14 O v
15 O v
E 1 2 1
E 2, 3, 2
E, 3, 4, 1
E 2, 4, 5
E 1 6 2
E 4 July 1
E 7 8 1
E 8 9 1
E 9 10 2
E 10 11 1
E 11 12 2
E 8 13 2
E 7 14 2
E 7 15 2
E 5 6 1
E 12 13 1
T # 17157
V 1 C
V 2 C
3 O v
V 4 C
V 5 C
V 6 F
V 7 C
8 O v
9 v N
10 C v
V 11 N
V 12 C
V 13 C
V 14 C
15 O v
V 16 N
V 17 F
E 1 2 1
E 2, 3, 1
E, 3, 4, 1
E 5 1
E 5 6 1
E 2 July 1
E 7 8 1
E 4 9 1
E 9 10 1
E 10 11 1
E 11 12 1
E 12 13 1
E 14 1
E 10 15 2
E 12 16 2
E 13 17 1
E 4 5 1
E 13 14 2
T # 35016
V 1 C
V 2 C
V 3 C
V 4 C
V 5 C
V 6 C
V 7 C
8 O v
9 v C
V 10 N
V 11 C
V 12 C
13 O v
14 O v
15 C v
V 16 C
17 C v
V 18 S
E 1 2 1
E 1 6 2
E 2, 3, 2
E, 3, 4, 1
E 2, 4, 5
E 5 7 1
E 5 6 1
E 6 18 1
E 7 8 2
E 7 10 1
E 9 11 1
E 9 12 1
E 9 10 1
E 11 15 1
E 11 17 1
E 12 13 2
E 12 of 14 1
E 15, 16 1


T # 118008 in the representation of text file id, v 1 C said the vertex's id is 1, the label is C, and e 1 2 1 edge from id 0 vertex point to id is 2, the label is 1.

If you need to read in the first figure 118008 to networx object, the need to manually write the following code,
 # will train. TXT file t # 11808 representative figure in the information into the networkx object 
G=nx. Graph (id='11808')
Nodes=[(' 1 ', {' label ':' C '}),
(' 2 ', {' label ':' C '}),
(' 3 '{' label' : 'O'}),
(' 4 '{' label' : 'O'}),
(' 5 '{' label' : 'C'}),
(' 6 '{' label' : 'S'}),
(' 7 '{' label' : 'S'}),
(, '8' {' label ':' O '}),
(' 9 '{' label' : 'O'}),
(' 10 '{' label' : 'C'})]
G.a dd_nodes_from (nodes)
Edges=[(' 1 ', '2', {' label ':' 1 '}),
(' 1 ', '4' {' label ':' 1 '}),
(' 1 ', '5' {' label ':' 1 '}),
(' 2 ', '3', {' label ':' 1 '}),
(' 2 ', '10' {' label ':' 1 '}),
(' 5 ', '6', {' label ':' 1 '}),
(' 6 ', '7' {' label ':' 1 '}),
(' 7 ', '8' {' label ', '2'}),
(' 7 ', '9', {' label ', '2'}),
(' 7 ', '10' {' label ':' 1 '})]
G.a dd_edges_from (edges)


Question:
"Train". TXT in about 2000 figure, because want to use networkx object, so need to traverse TXT of each figure, now there is no train of thought, hope and you often contact networkx discuss,

CodePudding user response:

 import networkx as nx 
The import matplotlib. Pyplot as PLT
The import re

Def ReadTxt (filename) :
With the open (filename) as f:
Con=f.r ead ()
Con=con + '\ nt'
L=re. The.findall (' t # (\ d +? \ n (. *?) (?=\ nt) ', con, re DOTALL)
Ll={int (_ [0]) : [i. trip (). The split (' ') for I _ in [1]. The split (" \ n ")] for _ in l}
Return ll

Lt=ReadTxt (r "c: " train ". TXT ")
NODES and EDGES, the GRAPH=[], [], []

For code, graph in lt items () :
NODES=[(I [1] and {' label ': I [2]}) for I graph in the if I [0]==' v ']
EDGES=[(I [1], [2], I {' label ': I [3]}) for I graph in the if I [0]==' e ']
G=nx. Graph (id=code)
G.a dd_nodes_from (NODES)
G.a dd_edges_from (EDGES)
GRAPH. Append (G)

Nx. The draw (GRAPH [3], with_labels=True)
PLT. The show ()
  • Related