Skip to content

Instantly share code, notes, and snippets.

@peterk
Created August 17, 2016 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterk/0b2508ca3274512105c1257c74e01450 to your computer and use it in GitHub Desktop.
Save peterk/0b2508ca3274512105c1257c74e01450 to your computer and use it in GitHub Desktop.
# coding: utf8
from openpyxl import Workbook, load_workbook
from openpyxl.compat import range
import networkx as nx
# Detta skript läser en NB-export i Excel och skriver en gexf-fil för vidare bearbetning i Gephi
wb = Workbook()
wb = load_workbook('bio2014.xlsx')
# använd första arket
ws = wb.worksheets[0]
G=nx.DiGraph()
# loop spreadsheet rows
for row in ws.iter_rows(row_offset=1):
# skip empty authors etc
if row[1].value and row[2].value and row[5].value:
# author
auth = row[1].value.strip().replace(" ;","")
G.add_node(auth, ttype="author")
# work
#work = row[2].value.strip()
#G.add_node(work, ttype="work")
# publ
publisher = row[5].value.strip().replace(" ;","")
G.add_node(publisher, ttype="publisher")
cats = []
# categories
for col in range(6, 33):
#if row[col].value:
if row[col].value and "[650]" in row[col].value and " sao" in row[col].value:
label = row[col].value.strip().replace("[650] ","").replace(" sao","")
G.add_node(label, ttype="cat")
#G.add_edge(work, row[col].value.strip())
G.add_edge(publisher, label)
# add edges
#G.add_edge(work, auth)
#G.add_edge(work, publisher)
G.add_edge(auth, publisher)
nx.write_gexf(G, "nbtest650.gexf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment