summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoss Barnowski <rossbar@berkeley.edu>2021-09-10 01:30:44 -0500
committerGitHub <noreply@github.com>2021-09-10 08:30:44 +0200
commit4d072ce7106d13ebce3cef6c4357a20181dc2886 (patch)
tree0d18827f9896833dd159541f460e0bc86bf7db9c
parent49331b2a2ed18c4e7a407dac2a78bb653af9a1ba (diff)
downloadnetworkx-4d072ce7106d13ebce3cef6c4357a20181dc2886.tar.gz
Support `comments=None` in read/parse edgelist (#5051)
* Add support for comments=None in parse_edgelist. * Update docstrings. * if instead of try/except.
-rw-r--r--networkx/readwrite/edgelist.py17
-rw-r--r--networkx/readwrite/tests/test_edgelist.py8
2 files changed, 18 insertions, 7 deletions
diff --git a/networkx/readwrite/edgelist.py b/networkx/readwrite/edgelist.py
index 239efdbe..57de2c5f 100644
--- a/networkx/readwrite/edgelist.py
+++ b/networkx/readwrite/edgelist.py
@@ -183,7 +183,8 @@ def parse_edgelist(
lines : list or iterator of strings
Input data in edgelist format
comments : string, optional
- Marker for comment lines. Default is `'#'`
+ Marker for comment lines. Default is `'#'`. To specify that no character
+ should be treated as a comment, use ``comments=None``.
delimiter : string, optional
Separator for node labels. Default is `None`, meaning any whitespace.
create_using : NetworkX graph constructor, optional (default=nx.Graph)
@@ -238,11 +239,12 @@ def parse_edgelist(
G = nx.empty_graph(0, create_using)
for line in lines:
- p = line.find(comments)
- if p >= 0:
- line = line[:p]
- if not line:
- continue
+ if comments is not None:
+ p = line.find(comments)
+ if p >= 0:
+ line = line[:p]
+ if not line:
+ continue
# split line, should have 2 or more
s = line.strip().split(delimiter)
if len(s) < 2:
@@ -314,7 +316,8 @@ def read_edgelist(
opened in 'rb' mode.
Filenames ending in .gz or .bz2 will be uncompressed.
comments : string, optional
- The character used to indicate the start of a comment.
+ The character used to indicate the start of a comment. To specify that
+ no character should be treated as a comment, use ``comments=None``.
delimiter : string, optional
The string used to separate values. The default is whitespace.
create_using : NetworkX graph constructor, optional (default=nx.Graph)
diff --git a/networkx/readwrite/tests/test_edgelist.py b/networkx/readwrite/tests/test_edgelist.py
index d237e0bf..b4672656 100644
--- a/networkx/readwrite/tests/test_edgelist.py
+++ b/networkx/readwrite/tests/test_edgelist.py
@@ -161,6 +161,14 @@ def test_parse_edgelist():
nx.parse_edgelist(lines, nodetype=int, data=(("weight", float),))
+def test_comments_None():
+ edgelist = ["node#1 node#2", "node#2 node#3"]
+ # comments=None supported to ignore all comment characters
+ G = nx.parse_edgelist(edgelist, comments=None)
+ H = nx.Graph([e.split(" ") for e in edgelist])
+ assert edges_equal(G.edges, H.edges)
+
+
class TestEdgelist:
@classmethod
def setup_class(cls):