diff options
author | Aric Hagberg <aric.hagberg@gmail.com> | 2014-06-02 20:20:43 -0600 |
---|---|---|
committer | Aric Hagberg <aric.hagberg@gmail.com> | 2014-06-02 20:20:43 -0600 |
commit | 091d148580a9461e8ed10d2e50146451edb49c0a (patch) | |
tree | db727f5fb06f9fa4d38daecf355dcae90b8054bf | |
parent | b3a17a08dce79c5332528df58e16a8994cac0d7d (diff) | |
download | networkx-091d148580a9461e8ed10d2e50146451edb49c0a.tar.gz |
Pajek reader can't handle encoded data
shlex.split() doesn't work for unicode. Tweak to work with Python2.x.
-rw-r--r-- | networkx/readwrite/pajek.py | 14 | ||||
-rw-r--r-- | networkx/readwrite/tests/test_pajek.py | 20 |
2 files changed, 32 insertions, 2 deletions
diff --git a/networkx/readwrite/pajek.py b/networkx/readwrite/pajek.py index f284c5e5..71aeb81b 100644 --- a/networkx/readwrite/pajek.py +++ b/networkx/readwrite/pajek.py @@ -172,7 +172,12 @@ def parse_pajek(lines): nodelabels={} l,nnodes=l.split() for i in range(int(nnodes)): - splitline=shlex.split(str(next(lines))) + l = next(lines) + try: + splitline=[x.decode('utf-8') for x in + shlex.split(make_str(l).encode('utf-8'))] + except: + splitline = shlex.split(str(l)) id,label=splitline[0:2] G.add_node(label) nodelabels[id]=label @@ -194,7 +199,12 @@ def parse_pajek(lines): # switch to directed with multiple arcs for each existing edge G=G.to_directed() for l in lines: - splitline=shlex.split(str(l)) + try: + splitline = [x.decode('utf-8') for x in + shlex.split(make_str(l).encode('utf-8'))] + except: + splitline = shlex.split(str(l)) + if len(splitline)<2: continue ui,vi=splitline[0:2] diff --git a/networkx/readwrite/tests/test_pajek.py b/networkx/readwrite/tests/test_pajek.py index 9e7732f0..18c95dbc 100644 --- a/networkx/readwrite/tests/test_pajek.py +++ b/networkx/readwrite/tests/test_pajek.py @@ -57,3 +57,23 @@ class TestPajek(object): other_lines = self.data.split('\n')[1:] data = line + '\n'.join(other_lines) G = parse_pajek(data) + + # def test_unicode(self): + # import io + # G = nx.Graph() + # try: # Python 3.x + # name1 = chr(2344) + chr(123) + chr(6543) + # name2 = chr(5543) + chr(1543) + chr(324) + # except ValueError: # Python 2.6+ + # name1 = unichr(2344) + unichr(123) + unichr(6543) + # name2 = unichr(5543) + unichr(1543) + unichr(324) + # G.add_edge(name1, 'Radiohead', attr_dict={'foo': name2}) + # fh = io.BytesIO() + # nx.write_pajek(G,fh) + # fh.seek(0) + # H=nx.read_pajek(fh) + # assert_equal(sorted(G.nodes()),sorted(H.nodes())) + # assert_equal( + # sorted(sorted(e) for e in G.edges()), + # sorted(sorted(e) for e in H.edges())) + # assert_equal(G.graph,H.graph) |