Skip to content

Instantly share code, notes, and snippets.

@peterk
Created January 18, 2016 17:19
Show Gist options
  • Save peterk/7a2bc60e12a051c51502 to your computer and use it in GitHub Desktop.
Save peterk/7a2bc60e12a051c51502 to your computer and use it in GitHub Desktop.
# coding: utf-8
import unicodecsv as csv
import networkx as nx
csvdata = csv.reader(open('query_result.csv', 'rb'), delimiter=',', encoding='utf-8')
next(csvdata) #skip headers
graph=nx.DiGraph(label="swepub")
for row in csvdata:
# Publication
pubid = row[0]
graph.add_node(pubid , label=row[11], type=u"Publication")
# Author
authid = row[15]
graph.add_node(authid, label=row[14], type=u"Author")
# Affiliation
affid = row[17].strip()
if affid == "":
affid = "Unknown"
graph.add_node(affid, label=affid, type=u"Affiliation")
#Channel
chanid = row[18].strip()
if chanid == "":
chanid = row[20]
graph.add_node(chanid, label=chanid, type=u"Channel")
#relations
graph.add_edge(authid, pubid)
graph.add_edge(authid, affid)
graph.add_edge(pubid, chanid)
# dump it
nx.write_gexf(graph, "swepub.gexf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment