diff options
author | ysitu <ysitu@users.noreply.github.com> | 2014-06-04 08:44:26 -0400 |
---|---|---|
committer | ysitu <ysitu@users.noreply.github.com> | 2014-06-04 08:44:26 -0400 |
commit | 171fa6bf26e349f1f6c4406001c701ff94d75507 (patch) | |
tree | a3d15c8ff48ac0375e27116ce8ff4e447839e2fb | |
parent | e6dd47220a99ec8bc227f08cee02ed59d1f0cc2c (diff) | |
parent | eedbe3095d93e71ba454b4200d9be78e6b885d06 (diff) | |
download | networkx-171fa6bf26e349f1f6c4406001c701ff94d75507.tar.gz |
Merge pull request #1190 from hagberg/pajek-utf8
Pajek utf8
-rw-r--r-- | networkx/readwrite/pajek.py | 18 | ||||
-rw-r--r-- | networkx/readwrite/tests/test_pajek.py | 18 |
2 files changed, 32 insertions, 4 deletions
diff --git a/networkx/readwrite/pajek.py b/networkx/readwrite/pajek.py index f284c5e5..d8fdc055 100644 --- a/networkx/readwrite/pajek.py +++ b/networkx/readwrite/pajek.py @@ -12,7 +12,7 @@ Format See http://vlado.fmf.uni-lj.si/pub/networks/pajek/doc/draweps.htm for format information. """ -# Copyright (C) 2008-2011 by +# Copyright (C) 2008-2014 by # Aric Hagberg <hagberg@lanl.gov> # Dan Schult <dschult@colgate.edu> # Pieter Swart <swart@lanl.gov> @@ -20,7 +20,7 @@ for format information. # BSD license. import networkx as nx from networkx.utils import is_string_like, open_file, make_str -__author__ = """Aric Hagberg (hagberg@lanl.gov)""" +__author__ = """Aric Hagberg <aric.hagberg@gmail.com>""" __all__ = ['read_pajek', 'parse_pajek', 'generate_pajek', 'write_pajek'] def generate_pajek(G): @@ -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 AttributeError: + 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 AttributeError: + 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..70b2d0be 100644 --- a/networkx/readwrite/tests/test_pajek.py +++ b/networkx/readwrite/tests/test_pajek.py @@ -57,3 +57,21 @@ 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_nodes_equal(G.nodes(), H.nodes()) + assert_edges_equal(G.edges(), H.edges()) + assert_equal(G.graph,H.graph) |