Created
June 11, 2024 22:18
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# %% | |
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