0
# Graph I/O
1
2
Reading and writing graphs in various file formats including adjacency lists, edge lists, GraphML, GML, GEXF, Pajek, and other standard formats. NetworkX provides comprehensive I/O support for interoperability with other graph analysis tools.
3
4
## Capabilities
5
6
### Edge List Format
7
8
Simple text format with one edge per line, optionally with edge attributes.
9
10
```python { .api }
11
def read_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, data=True, edgetype=None, encoding='utf-8'):
12
"""
13
Read graph from edge list file.
14
15
Parameters:
16
- path: File path or file handle
17
- comments: Character marking comment lines
18
- delimiter: String used to separate values
19
- create_using: Graph constructor
20
- nodetype: Convert node identifiers to this type
21
- data: If True, load edge attributes
22
- edgetype: Convert edge data to this type
23
- encoding: Text encoding
24
25
Returns:
26
NetworkX graph
27
"""
28
29
def write_edgelist(G, path, comments='#', delimiter=' ', data=True, encoding='utf-8'):
30
"""Write graph to edge list file."""
31
32
def read_weighted_edgelist(path, comments='#', delimiter=None, create_using=None, nodetype=None, encoding='utf-8'):
33
"""Read graph from weighted edge list file."""
34
35
def write_weighted_edgelist(G, path, comments='#', delimiter=' ', encoding='utf-8'):
36
"""Write graph to weighted edge list file."""
37
```
38
39
### Adjacency List Format
40
41
Text format with one line per node listing its neighbors.
42
43
```python { .api }
44
def read_adjlist(path, comments='#', delimiter=None, create_using=None, nodetype=None, encoding='utf-8'):
45
"""
46
Read graph from adjacency list file.
47
48
Parameters:
49
- path: File path or file handle
50
- comments: Character marking comment lines
51
- delimiter: String separating node and neighbors
52
- create_using: Graph constructor
53
- nodetype: Convert nodes to this type
54
- encoding: Text encoding
55
56
Returns:
57
NetworkX graph
58
"""
59
60
def write_adjlist(G, path, comments='#', delimiter=' ', encoding='utf-8'):
61
"""Write graph to adjacency list file."""
62
63
def read_multiline_adjlist(path, comments='#', delimiter=None, create_using=None, nodetype=None, edgetype=None, encoding='utf-8'):
64
"""Read graph from multiline adjacency list file."""
65
66
def write_multiline_adjlist(G, path, delimiter=' ', comments='#', encoding='utf-8'):
67
"""Write graph to multiline adjacency list file."""
68
```
69
70
### GraphML Format
71
72
XML-based format supporting rich graph, node, and edge attributes.
73
74
```python { .api }
75
def read_graphml(path, node_type=str, edge_key_type=str, encoding='utf-8'):
76
"""
77
Read graph from GraphML file.
78
79
Parameters:
80
- path: File path or file handle
81
- node_type: Convert node IDs to this type
82
- edge_key_type: Convert edge keys to this type (for multigraphs)
83
- encoding: Text encoding
84
85
Returns:
86
NetworkX graph with attributes preserved
87
"""
88
89
def write_graphml(G, path, encoding='utf-8', prettyprint=True, infer_numeric_types=False, named_key_ids=False, edge_id_from_attribute=None):
90
"""Write graph to GraphML file."""
91
92
def write_graphml_xml(G, path, encoding='utf-8', prettyprint=True, infer_numeric_types=False, named_key_ids=False, edge_id_from_attribute=None):
93
"""Write graph to GraphML XML format."""
94
95
def parse_graphml(graphml_string, node_type=str, edge_key_type=str):
96
"""Parse GraphML from string."""
97
```
98
99
### GML Format
100
101
Graph Modeling Language, a text-based format with hierarchical structure.
102
103
```python { .api }
104
def read_gml(path, label='label', destringizer=None, encoding='utf-8'):
105
"""
106
Read graph from GML file.
107
108
Parameters:
109
- path: File path or file handle
110
- label: If not None, node attribute for node labels
111
- destringizer: Function to convert string values
112
- encoding: Text encoding
113
114
Returns:
115
NetworkX graph
116
"""
117
118
def write_gml(G, path, stringizer=None):
119
"""Write graph to GML file."""
120
121
def parse_gml(lines, label='label', destringizer=None):
122
"""Parse GML from lines of text."""
123
124
def generate_gml(G, stringizer=None):
125
"""Generate GML representation of graph."""
126
```
127
128
### GEXF Format
129
130
Graph Exchange XML Format supporting dynamic graphs and visualization.
131
132
```python { .api }
133
def read_gexf(path, node_type=None, relabel=False, version='1.2draft', encoding='utf-8'):
134
"""
135
Read graph from GEXF file.
136
137
Parameters:
138
- path: File path or file handle
139
- node_type: Convert node IDs to this type
140
- relabel: If True, relabel nodes to integers
141
- version: GEXF file version
142
- encoding: Text encoding
143
144
Returns:
145
NetworkX graph
146
"""
147
148
def write_gexf(G, path, encoding='utf-8', prettyprint=True, version='1.2draft'):
149
"""Write graph to GEXF file."""
150
151
def relabel_gexf_graph(G):
152
"""Relabel graph nodes to be suitable for GEXF."""
153
```
154
155
### Pajek Format
156
157
Format used by Pajek software for large network analysis.
158
159
```python { .api }
160
def read_pajek(path, encoding='UTF-8'):
161
"""
162
Read graph from Pajek file.
163
164
Parameters:
165
- path: File path or file handle
166
- encoding: Text encoding
167
168
Returns:
169
NetworkX graph with node and edge attributes
170
"""
171
172
def write_pajek(G, path, encoding='UTF-8'):
173
"""Write graph to Pajek file."""
174
175
def parse_pajek(lines):
176
"""Parse Pajek format from lines of text."""
177
178
def generate_pajek(G):
179
"""Generate Pajek representation of graph."""
180
```
181
182
### LEDA Format
183
184
Format used by LEDA (Library of Efficient Data types and Algorithms).
185
186
```python { .api }
187
def read_leda(path, encoding='utf-8'):
188
"""
189
Read graph from LEDA file.
190
191
Parameters:
192
- path: File path or file handle
193
- encoding: Text encoding
194
195
Returns:
196
NetworkX graph
197
"""
198
199
def write_leda(G, path, encoding='utf-8'):
200
"""Write graph to LEDA file."""
201
202
def parse_leda(lines):
203
"""Parse LEDA format from lines."""
204
```
205
206
### Graph6 and Sparse6 Formats
207
208
Compact binary formats for simple graphs.
209
210
```python { .api }
211
def read_graph6(path):
212
"""
213
Read graphs from Graph6 file.
214
215
Parameters:
216
- path: File path or file handle
217
218
Returns:
219
Generator of NetworkX graphs
220
"""
221
222
def write_graph6(G, path, nodes=None, header=True):
223
"""Write graph to Graph6 file."""
224
225
def from_graph6_bytes(string):
226
"""Create graph from Graph6 byte string."""
227
228
def to_graph6_bytes(G, nodes=None, header=True):
229
"""Convert graph to Graph6 byte string."""
230
231
def read_sparse6(path):
232
"""Read graphs from Sparse6 file."""
233
234
def write_sparse6(G, path, nodes=None, header=True):
235
"""Write graph to Sparse6 file."""
236
237
def from_sparse6_bytes(string):
238
"""Create graph from Sparse6 byte string."""
239
240
def to_sparse6_bytes(G, nodes=None, header=True):
241
"""Convert graph to Sparse6 byte string."""
242
```
243
244
### Python Pickle Format
245
246
Native Python serialization for complete graph preservation.
247
248
```python { .api }
249
def read_gpickle(path):
250
"""
251
Read graph from Python pickle file.
252
253
Parameters:
254
- path: File path or file handle
255
256
Returns:
257
NetworkX graph with all attributes preserved
258
"""
259
260
def write_gpickle(G, path, protocol=2):
261
"""Write graph to Python pickle file."""
262
```
263
264
### JSON Graph Format
265
266
JavaScript Object Notation format for web applications.
267
268
```python { .api }
269
def node_link_data(G, attrs=None):
270
"""
271
Convert graph to node-link JSON format.
272
273
Parameters:
274
- G: NetworkX graph
275
- attrs: Dictionary of node/edge/graph attribute names
276
277
Returns:
278
Dictionary in node-link format suitable for JSON
279
"""
280
281
def node_link_graph(data, directed=False, multigraph=True, attrs=None):
282
"""Create graph from node-link JSON data."""
283
284
def adjacency_data(G, attrs=None):
285
"""Convert graph to adjacency JSON format."""
286
287
def adjacency_graph(data, directed=False, multigraph=True, attrs=None):
288
"""Create graph from adjacency JSON data."""
289
290
def tree_data(G, root, attrs=None, ident='id', children='children'):
291
"""Convert tree to nested JSON format."""
292
293
def tree_graph(data, attrs=None, ident='id', children='children'):
294
"""Create tree from nested JSON data."""
295
```
296
297
### Shapefile Format
298
299
Geographic format for spatial networks.
300
301
```python { .api }
302
def read_shp(path, simplify=True, geom_attrs=True, strict=True):
303
"""
304
Read graph from ESRI Shapefile.
305
306
Parameters:
307
- path: Path to shapefile
308
- simplify: If True, simplify linestring geometry
309
- geom_attrs: Include geometry as node/edge attributes
310
- strict: If True, raise exception on geometry errors
311
312
Returns:
313
NetworkX graph with geographic coordinates
314
"""
315
316
def write_shp(G, outdir):
317
"""Write graph to ESRI Shapefile format."""
318
```
319
320
## Usage Examples
321
322
### Basic File I/O
323
324
```python
325
import networkx as nx
326
327
# Create sample graph
328
G = nx.karate_club_graph()
329
330
# Write to different formats
331
nx.write_edgelist(G, "karate.edgelist")
332
nx.write_adjlist(G, "karate.adjlist")
333
nx.write_gml(G, "karate.gml")
334
nx.write_graphml(G, "karate.graphml")
335
nx.write_pajek(G, "karate.net")
336
337
# Read back from files
338
G1 = nx.read_edgelist("karate.edgelist", nodetype=int)
339
G2 = nx.read_adjlist("karate.adjlist", nodetype=int)
340
G3 = nx.read_gml("karate.gml")
341
G4 = nx.read_graphml("karate.graphml")
342
G5 = nx.read_pajek("karate.net")
343
344
print(f"Original: {G.number_of_nodes()} nodes, {G.number_of_edges()} edges")
345
print(f"Edgelist: {G1.number_of_nodes()} nodes, {G1.number_of_edges()} edges")
346
print(f"GML: {G3.number_of_nodes()} nodes, {G3.number_of_edges()} edges")
347
```
348
349
### Working with Attributes
350
351
```python
352
import networkx as nx
353
354
# Create graph with attributes
355
G = nx.Graph()
356
G.graph['name'] = 'Sample Network'
357
G.graph['created'] = '2024'
358
359
G.add_node(1, name='Alice', age=25, city='New York')
360
G.add_node(2, name='Bob', age=30, city='Boston')
361
G.add_node(3, name='Charlie', age=35, city='Chicago')
362
363
G.add_edge(1, 2, weight=0.8, relationship='friend', since=2020)
364
G.add_edge(2, 3, weight=0.6, relationship='colleague', since=2021)
365
G.add_edge(1, 3, weight=0.4, relationship='acquaintance', since=2022)
366
367
# Save to formats that preserve attributes
368
nx.write_gml(G, "network_with_attrs.gml")
369
nx.write_graphml(G, "network_with_attrs.graphml")
370
nx.write_gpickle(G, "network_with_attrs.pickle")
371
372
# Read back and verify attributes are preserved
373
G_gml = nx.read_gml("network_with_attrs.gml")
374
G_graphml = nx.read_graphml("network_with_attrs.graphml")
375
G_pickle = nx.read_gpickle("network_with_attrs.pickle")
376
377
print("GML graph attributes:", G_gml.graph)
378
print("GML node 1 attributes:", G_gml.nodes[1])
379
print("GML edge (1,2) attributes:", G_gml[1][2])
380
381
print("GraphML node types:", [type(n) for n in G_graphml.nodes()])
382
print("Pickle preserves exact types:", [type(n) for n in G_pickle.nodes()])
383
```
384
385
### JSON Format for Web Applications
386
387
```python
388
import networkx as nx
389
import json
390
391
# Create graph
392
G = nx.karate_club_graph()
393
394
# Add some attributes
395
for node in G.nodes():
396
G.nodes[node]['group'] = 'A' if G.degree(node) > 5 else 'B'
397
398
# Convert to JSON formats
399
node_link_data = nx.node_link_data(G)
400
adjacency_data = nx.adjacency_data(G)
401
402
# Save as JSON files
403
with open('karate_nodelink.json', 'w') as f:
404
json.dump(node_link_data, f, indent=2)
405
406
with open('karate_adjacency.json', 'w') as f:
407
json.dump(adjacency_data, f, indent=2)
408
409
# Read back from JSON
410
with open('karate_nodelink.json', 'r') as f:
411
data = json.load(f)
412
G_nodelink = nx.node_link_graph(data)
413
414
with open('karate_adjacency.json', 'r') as f:
415
data = json.load(f)
416
G_adjacency = nx.adjacency_graph(data)
417
418
print(f"Node-link format: {G_nodelink.number_of_nodes()} nodes")
419
print(f"Adjacency format: {G_adjacency.number_of_nodes()} nodes")
420
print("Node attributes preserved:", G_nodelink.nodes[0])
421
```
422
423
### Weighted Edge Lists
424
425
```python
426
import networkx as nx
427
import numpy as np
428
429
# Create weighted graph
430
G = nx.complete_graph(5)
431
for (u, v) in G.edges():
432
G[u][v]['weight'] = np.random.random()
433
434
# Write weighted edge list
435
nx.write_weighted_edgelist(G, "weighted_complete.edgelist")
436
437
# Examine the file format
438
with open("weighted_complete.edgelist", 'r') as f:
439
print("First few lines of weighted edge list:")
440
for i, line in enumerate(f):
441
print(line.strip())
442
if i >= 5:
443
break
444
445
# Read back weighted graph
446
G_weighted = nx.read_weighted_edgelist("weighted_complete.edgelist", nodetype=int)
447
print(f"Loaded graph: {G_weighted.number_of_nodes()} nodes, {G_weighted.number_of_edges()} edges")
448
449
# Verify edge weights
450
edge = list(G_weighted.edges())[0]
451
print(f"Sample edge {edge} weight: {G_weighted[edge[0]][edge[1]]['weight']}")
452
```
453
454
### Large Graph Formats
455
456
```python
457
import networkx as nx
458
459
# Create larger graph
460
G = nx.erdos_renyi_graph(1000, 0.01, seed=42)
461
462
# Compare file sizes and speeds for different formats
463
import time
464
import os
465
466
formats = [
467
('edgelist', nx.write_edgelist, nx.read_edgelist),
468
('adjlist', nx.write_adjlist, nx.read_adjlist),
469
('gpickle', nx.write_gpickle, nx.read_gpickle),
470
('gml', nx.write_gml, nx.read_gml)
471
]
472
473
for name, write_func, read_func in formats:
474
filename = f"large_graph.{name}"
475
476
# Time writing
477
start = time.time()
478
if name == 'gml':
479
write_func(G, filename)
480
else:
481
write_func(G, filename)
482
write_time = time.time() - start
483
484
# Get file size
485
file_size = os.path.getsize(filename)
486
487
# Time reading
488
start = time.time()
489
if name in ['edgelist', 'adjlist']:
490
G_read = read_func(filename, nodetype=int)
491
else:
492
G_read = read_func(filename)
493
read_time = time.time() - start
494
495
print(f"{name:10} | Size: {file_size:8d} bytes | "
496
f"Write: {write_time:.3f}s | Read: {read_time:.3f}s")
497
```
498
499
### Compact Binary Formats
500
501
```python
502
import networkx as nx
503
504
# Create several small graphs
505
graphs = [
506
nx.complete_graph(5),
507
nx.cycle_graph(6),
508
nx.path_graph(8),
509
nx.star_graph(4)
510
]
511
512
# Write to Graph6 format (one file, multiple graphs)
513
nx.write_graph6(graphs, "small_graphs.g6")
514
515
# Read all graphs back
516
graphs_read = list(nx.read_graph6("small_graphs.g6"))
517
print(f"Wrote {len(graphs)} graphs, read {len(graphs_read)} graphs")
518
519
# Compare individual graphs
520
for i, (orig, read) in enumerate(zip(graphs, graphs_read)):
521
print(f"Graph {i}: Original {orig.number_of_edges()} edges, "
522
f"Read {read.number_of_edges()} edges, "
523
f"Isomorphic: {nx.is_isomorphic(orig, read)}")
524
525
# Show compact representation
526
with open("small_graphs.g6", 'r') as f:
527
print("\nGraph6 file content:")
528
print(f.read())
529
```