# UN-DIRECTED
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from([("jack","sally"),("jack","diane"),("jack","joe"),("joe","diane"),("diane","sally"),("jack","jill")])
fig, ax = plt.subplots()
fig.set_size_inches(12, 12)
nx.draw(G,
with_labels=True,
# labels=labels,
node_color='blue',
width=3,
node_size=4000,
font_color='white',
font_size=18,
ax=ax
)
plt.show()
# START FRESH (delete output folder and re-create)
import os
import shutil
dirpath="./output-1"
if os.path.exists(dirpath) and os.path.isdir(dirpath):
shutil.rmtree(dirpath)
os.mkdir(dirpath)
# EXPORT
nx.write_adjlist(G, dirpath+'/example.adjlist')
nx.write_multiline_adjlist(G, dirpath+'/example.mult-adjlist')
nx.write_edgelist(G, dirpath+'/example.edgelist')
nx.write_gml(G, dirpath+'/example.gml')
nx.write_gexf(G, dirpath+'/example.gexf')
nx.write_graphml(G, dirpath+'/example.graphml')Network file formats
Common network file formats
- The formats in orange can be used by both software packages

Basic I/O in NetworkX
Other file-formats can be loaded or exported with NetworkX using the following commands
Import
G=nx.read_adjlist('file_path')
G=nx.read_multiline_adjlist('file_path')
G=nx.read_edgelist('file_path')
G=nx.read_gml('file_path')
G=nx.read_gexf('file_path')
G=nx.read_graphml('file_path')
Export
nx.write_adjlist(G, 'file_path')
nx.write_multiline_adjlist(G, 'file_path')
nx.write_edgelist(G, 'file_path')
nx.write_gml(G, 'file_path')
nx.write_gexf(G, 'file_path')
nx.write_graphml(G, 'file_path')
For more information see: https://networkx.org/documentation/stable/reference/readwrite/index.html
Example-1: Un-directed graph I/O
Generate and save data
Inspect files
import os
dir="output-1/"
for file in os.listdir(dir):
print("---------- FILE="+dir+file + " ----------")
with open(dir+file, 'r') as f:
print(f.read())---------- FILE=output-1/example.gexf ----------
<?xml version='1.0' encoding='utf-8'?>
<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd" version="1.2">
<meta lastmodifieddate="2023-06-13">
<creator>NetworkX 3.0</creator>
</meta>
<graph defaultedgetype="undirected" mode="static" name="">
<nodes>
<node id="jack" label="jack" />
<node id="sally" label="sally" />
<node id="diane" label="diane" />
<node id="joe" label="joe" />
<node id="jill" label="jill" />
</nodes>
<edges>
<edge source="jack" target="sally" id="0" />
<edge source="jack" target="diane" id="1" />
<edge source="jack" target="joe" id="2" />
<edge source="jack" target="jill" id="3" />
<edge source="sally" target="diane" id="4" />
<edge source="diane" target="joe" id="5" />
</edges>
</graph>
</gexf>
---------- FILE=output-1/example.graphml ----------
<?xml version='1.0' encoding='utf-8'?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"><graph edgedefault="undirected"><node id="jack"/>
<node id="sally"/>
<node id="diane"/>
<node id="joe"/>
<node id="jill"/>
<edge source="jack" target="sally"/>
<edge source="jack" target="diane"/>
<edge source="jack" target="joe"/>
<edge source="jack" target="jill"/>
<edge source="sally" target="diane"/>
<edge source="diane" target="joe"/>
</graph></graphml>
---------- FILE=output-1/example.adjlist ----------
#/Users/jameshickman/miniconda3/envs/anly503/lib/python3.10/site-packages/ipykernel_launcher.py --ip=127.0.0.1 --stdin=9013 --control=9011 --hb=9010 --Session.signature_scheme="hmac-sha256" --Session.key=b"71db96bb-c10e-405c-92f4-94d0fcc7db04" --shell=9012 --transport="tcp" --iopub=9014 --f=/Users/jameshickman/Library/Jupyter/runtime/kernel-v2-71309WONFzr0B61l1.json
# GMT Tue Jun 13 18:05:19 2023
#
jack sally diane joe jill
sally diane
diane joe
joe
jill
---------- FILE=output-1/example.gml ----------
graph [
node [
id 0
label "jack"
]
node [
id 1
label "sally"
]
node [
id 2
label "diane"
]
node [
id 3
label "joe"
]
node [
id 4
label "jill"
]
edge [
source 0
target 1
]
edge [
source 0
target 2
]
edge [
source 0
target 3
]
edge [
source 0
target 4
]
edge [
source 1
target 2
]
edge [
source 2
target 3
]
]
---------- FILE=output-1/example.mult-adjlist ----------
#/Users/jameshickman/miniconda3/envs/anly503/lib/python3.10/site-packages/ipykernel_launcher.py --ip=127.0.0.1 --stdin=9013 --control=9011 --hb=9010 --Session.signature_scheme="hmac-sha256" --Session.key=b"71db96bb-c10e-405c-92f4-94d0fcc7db04" --shell=9012 --transport="tcp" --iopub=9014 --f=/Users/jameshickman/Library/Jupyter/runtime/kernel-v2-71309WONFzr0B61l1.json
# GMT Tue Jun 13 18:05:19 2023
#
jack 4
sally {}
diane {}
joe {}
jill {}
sally 1
diane {}
diane 1
joe {}
joe 0
jill 0
---------- FILE=output-1/example.edgelist ----------
jack sally {}
jack diane {}
jack joe {}
jack jill {}
sally diane {}
diane joe {}
Read
print("ORIGINAL")
print(G.edges)
# READ
print("READ")
G=nx.read_adjlist(dirpath+'/example.adjlist'); print(G.edges)
G=nx.read_adjlist(dirpath+'/example.adjlist'); #print(G.edges)
G=nx.read_multiline_adjlist(dirpath+'/example.mult-adjlist'); #print(G.edges)
G=nx.read_edgelist(dirpath+'/example.edgelist'); #print(G.edges)
G=nx.read_gml(dirpath+'/example.gml'); #print(G.edges)
G=nx.read_gexf(dirpath+'/example.gexf'); #print(G.edges)
G=nx.read_graphml(dirpath+'/example.graphml'); #print(G.edges)ORIGINAL
[('jack', 'sally'), ('jack', 'diane'), ('jack', 'joe'), ('jack', 'jill'), ('sally', 'diane'), ('diane', 'joe')]
READ
[('jack', 'sally'), ('jack', 'diane'), ('jack', 'joe'), ('jack', 'jill'), ('sally', 'diane'), ('diane', 'joe')]
Example-2: Directed graph I/O
Generate and save data
# DIRECTED
import networkx as nx
import matplotlib.pyplot as plt
G = nx.DiGraph()
G.add_edges_from([("jack","sally"),("jack","diane"),("jack","joe"),("joe","diane"),("diane","sally"),("jack","jill")])
fig, ax = plt.subplots()
fig.set_size_inches(12, 12)
nx.draw(G,
with_labels=True,
# labels=labels,
node_color='blue',
width=3,
node_size=4000,
font_color='white',
font_size=18,
ax=ax
)
plt.show()
# START FRESH (delete output folder and re-create)
import os
import shutil
dirpath="./output-2"
if os.path.exists(dirpath) and os.path.isdir(dirpath):
shutil.rmtree(dirpath)
os.mkdir(dirpath)
#EXPORT
nx.write_adjlist(G, dirpath+'/directed.adjlist')
nx.write_multiline_adjlist(G, dirpath+'/directed.mult-adjlist')
nx.write_edgelist(G, dirpath+'/directed.edgelist')
nx.write_gml(G, dirpath+'/directed.gml')
nx.write_gexf(G, dirpath+'/directed.gexf')
nx.write_graphml(G, dirpath+'/directed.graphml')Inspect files
import os
dir="output-2/"
for file in os.listdir(dir):
print("---------- FILE="+dir+file + " ----------")
with open(dir+file, 'r') as f:
print(f.read())---------- FILE=output-2/directed.adjlist ----------
#/Users/jameshickman/miniconda3/envs/anly503/lib/python3.10/site-packages/ipykernel_launcher.py --ip=127.0.0.1 --stdin=9013 --control=9011 --hb=9010 --Session.signature_scheme="hmac-sha256" --Session.key=b"71db96bb-c10e-405c-92f4-94d0fcc7db04" --shell=9012 --transport="tcp" --iopub=9014 --f=/Users/jameshickman/Library/Jupyter/runtime/kernel-v2-71309WONFzr0B61l1.json
# GMT Tue Jun 13 18:05:20 2023
#
jack sally diane joe jill
sally
diane sally
joe diane
jill
---------- FILE=output-2/directed.edgelist ----------
jack sally {}
jack diane {}
jack joe {}
jack jill {}
diane sally {}
joe diane {}
---------- FILE=output-2/directed.mult-adjlist ----------
#/Users/jameshickman/miniconda3/envs/anly503/lib/python3.10/site-packages/ipykernel_launcher.py --ip=127.0.0.1 --stdin=9013 --control=9011 --hb=9010 --Session.signature_scheme="hmac-sha256" --Session.key=b"71db96bb-c10e-405c-92f4-94d0fcc7db04" --shell=9012 --transport="tcp" --iopub=9014 --f=/Users/jameshickman/Library/Jupyter/runtime/kernel-v2-71309WONFzr0B61l1.json
# GMT Tue Jun 13 18:05:20 2023
#
jack 4
sally {}
diane {}
joe {}
jill {}
sally 0
diane 1
sally {}
joe 1
diane {}
jill 0
---------- FILE=output-2/directed.gexf ----------
<?xml version='1.0' encoding='utf-8'?>
<gexf xmlns="http://www.gexf.net/1.2draft" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd" version="1.2">
<meta lastmodifieddate="2023-06-13">
<creator>NetworkX 3.0</creator>
</meta>
<graph defaultedgetype="directed" mode="static" name="">
<nodes>
<node id="jack" label="jack" />
<node id="sally" label="sally" />
<node id="diane" label="diane" />
<node id="joe" label="joe" />
<node id="jill" label="jill" />
</nodes>
<edges>
<edge source="jack" target="sally" id="0" />
<edge source="jack" target="diane" id="1" />
<edge source="jack" target="joe" id="2" />
<edge source="jack" target="jill" id="3" />
<edge source="diane" target="sally" id="4" />
<edge source="joe" target="diane" id="5" />
</edges>
</graph>
</gexf>
---------- FILE=output-2/directed.gml ----------
graph [
directed 1
node [
id 0
label "jack"
]
node [
id 1
label "sally"
]
node [
id 2
label "diane"
]
node [
id 3
label "joe"
]
node [
id 4
label "jill"
]
edge [
source 0
target 1
]
edge [
source 0
target 2
]
edge [
source 0
target 3
]
edge [
source 0
target 4
]
edge [
source 2
target 1
]
edge [
source 3
target 2
]
]
---------- FILE=output-2/directed.graphml ----------
<?xml version='1.0' encoding='utf-8'?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd"><graph edgedefault="directed"><node id="jack"/>
<node id="sally"/>
<node id="diane"/>
<node id="joe"/>
<node id="jill"/>
<edge source="jack" target="sally"/>
<edge source="jack" target="diane"/>
<edge source="jack" target="joe"/>
<edge source="jack" target="jill"/>
<edge source="diane" target="sally"/>
<edge source="joe" target="diane"/>
</graph></graphml>
Read
# PRINT
print("ORIGINAL")
print(G.edges)
#READ
print("READ")
G=nx.read_adjlist(dirpath+'/directed.adjlist'); print(G.edges)
G=nx.read_multiline_adjlist(dirpath+'/directed.mult-adjlist'); #print(G.edges)
G=nx.read_edgelist(dirpath+'/directed.edgelist'); #print(G.edges)
G=nx.read_gml(dirpath+'/directed.gml'); #print(G.edges)
G=nx.read_gexf(dirpath+'/directed.gexf'); #print(G.edges)
G=nx.read_graphml(dirpath+'/directed.graphml'); #print(G.edges)ORIGINAL
[('jack', 'sally'), ('jack', 'diane'), ('jack', 'joe'), ('jack', 'jill'), ('diane', 'sally'), ('joe', 'diane')]
READ
[('jack', 'sally'), ('jack', 'diane'), ('jack', 'joe'), ('jack', 'jill'), ('sally', 'diane'), ('diane', 'joe')]