-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgio.py
More file actions
42 lines (32 loc) · 892 Bytes
/
Copy pathgio.py
File metadata and controls
42 lines (32 loc) · 892 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 28 22:01:10 2016
@author: Oliver
"""
import networkx as nx
def read(filename):
file = open(filename)
G = None
Gs = {}
def end(G):
if G != None:
Gs[G] = freq
return nx.MultiGraph()
for line in file:
if line.startswith("FREQ"):
G = end(G)
freq = int(line.split()[1])
else:
G.add_edge(*map(int,line.split()))
G = end(G)
file.close()
return Gs
def write(filename, Gs):
file = open(filename, 'w')
if type(Gs) is list:
gen = ('GRAPH\n{}'.format( nx.generate_edgelist(G,data=False)) for G in Gs)
else:
gen = ('FREQUENCY %d\n'%freq + '\n'.join(nx.generate_edgelist(
G,data=False))+ '\n' for G,freq in Gs.items() )
file.writelines(gen)
file.close()