MCPcopy Create free account
hub / github.com/easy-graph/Easy-Graph / add_edges_from_file

Method add_edges_from_file

easygraph/classes/directed_graph.py:806–859  ·  view source on GitHub ↗

Added edges from file For example, txt files, Each line is in form like: a b 23.0 which denotes an edge `a → b` with weight 23.0. Parameters ---------- file : string The file path. weighted : boolean, optional (default :

(self, file, weighted=False)

Source from the content-addressed store, hash-verified

804 self._pred[v][u] = datadict
805
806 def add_edges_from_file(self, file, weighted=False):
807 """Added edges from file
808 For example, txt files,
809
810 Each line is in form like:
811 a b 23.0
812 which denotes an edge `a → b` with weight 23.0.
813
814 Parameters
815 ----------
816 file : string
817 The file path.
818
819 weighted : boolean, optional (default : False)
820 If the file consists of weight information, set `True`.
821 The weight key will be set as 'weight'.
822
823 Examples
824 --------
825
826 If `./club_network.txt` is:
827
828 Jack Mary 23.0
829
830 Mary Tom 15.0
831
832 Tom Ben 20.0
833
834 Then add them to *G*
835
836 >>> G.add_edges_from_file(file='./club_network.txt', weighted=True)
837
838
839 """
840 import re
841
842 with open(file, "r") as fp:
843 edges = fp.readlines()
844 if weighted:
845 for edge in edges:
846 edge = re.sub(",", " ", edge)
847 edge = edge.split()
848 try:
849 self.add_edge(edge[0], edge[1], weight=float(edge[2]))
850 except:
851 pass
852 else:
853 for edge in edges:
854 edge = re.sub(",", " ", edge)
855 edge = edge.split()
856 try:
857 self.add_edge(edge[0], edge[1])
858 except:
859 pass
860
861 def _add_one_edge(self, u_of_edge, v_of_edge, edge_attr: dict = {}):
862 u, v = u_of_edge, v_of_edge

Callers

nothing calls this directly

Calls 1

add_edgeMethod · 0.95

Tested by

no test coverage detected