Skip to content

Instantly share code, notes, and snippets.

@mys721tx
Created June 11, 2024 22:18
Show Gist options
  • Save mys721tx/2db94ef9710488f26d36313755e6f658 to your computer and use it in GitHub Desktop.
Save mys721tx/2db94ef9710488f26d36313755e6f658 to your computer and use it in GitHub Desktop.
Show the namesake relationship of every town named "Canton" in the United States
# %%
import csv
import networkx as nx
from networkx.drawing.nx_agraph import write_dot
# %%
# Create a directed graph
G = nx.DiGraph()
# Open and read the TSV file
with open("cantons.tsv", "r") as file:
reader = csv.DictReader(file, delimiter="\t")
for row in reader:
# Add an edge between the nodes specified in each row
G.add_edge(row["name"], row["namesake"])
# Now G is a directed graph representing the data in the TSV file
write_dot(G, "cantons.dot")
# %%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment