networkx plot random graph Error

Jason Swails jason.swails at gmail.com
Thu Jul 17 05:24:37 CEST 2014


On Jul 15, 2014, at 3:11 AM, u2107 <phani.lav at gmail.com> wrote:

> I am trying to read a file with 3 columns with col 1 and 2 as nodes/edges and column 3 as weight (value with decimal)
> 
> I am trying to execute this code 
> 
> 
> import networkx as nx
> 
> 
> G = nx.read_edgelist('file.txt', data=[("weight")])
> G.edges(data=True)
> 
> edge_labels = dict(((u, v), d["weight"]) for u, v, d in G.edges(data=True))
> pos = nx.random_layout(G)
> nx.draw(G, pos)
> nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
> 
> import matplotlib.pyplot as plt; plt.show()
> 
> 
> 
> Error: 
> 
> File "graph_one.py", line 4, in <module>
>    G = nx.read_edgelist('File.txt', data=[("weight")])
>  File "<string>", line 2, in read_edgelist
>  File "/usr/local/lib/python2.7/dist-packages/networkx-1.7-py2.7.egg/networkx/utils/decorators.py", line 241, in _open_file
>    fobj = _dispatch_dict[ext](path, mode=mode)
> IOError: [Errno 2] No such file or directory: 'contactUSC_nodes_diff_small.txt'

This looks like some file didn't exist which probably should have.  The next line looks like you may have fixed the problem and then run the command again?  Is this part of the error message?  Including stuff like this is confusing (to me, at least).

> lavanya at peerformance:~/Desktop/INRIA_Papers/Python$ python graph_one.py
> Traceback (most recent call last):
>  File "graph_one.py", line 4, in <module>
>    G = nx.read_edgelist('contactUSC_node_diff_small.txt', data=[("weight")])
>  File "<string>", line 2, in read_edgelist
>  File "/usr/local/lib/python2.7/dist-packages/networkx-1.7-py2.7.egg/networkx/utils/decorators.py", line 263, in _open_file
>    result = func(*new_args, **kwargs)
>  File "/usr/local/lib/python2.7/dist-packages/networkx-1.7-py2.7.egg/networkx/readwrite/edgelist.py", line 367, in read_edgelist
>    data=data)
>  File "/usr/local/lib/python2.7/dist-packages/networkx-1.7-py2.7.egg/networkx/readwrite/edgelist.py", line 286, in parse_edgelist
>    for (edge_key,edge_type),edge_value in zip(data,d):
> 
> ValueError: too many values to unpack

Collections in python can be "unpacked" using a specific syntax that assigns a tuple of individual variables (or names) to a collection (these can be nested).  If there are too many or too few single names, a ValueError gets thrown.  Consider:

py> col = [1, 2, 3]
py> a, b, c = col
py> print("{0} {1} {2}".format(a, b, c))
1 2 3
py> a, b = col
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
py> a, b, c, d = col
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 3 values to unpack

The traceback for this exception ends inside the networkx code and either indicates a bug in the networkx code or an input error that is not gracefully handled.

My suggestions are twofold:

1) Upgrade to the latest release.  If this is a bug, it's very possible that it was fixed in later releases (1.7 is over 2 years old and there have been a couple major releases since then.

2) Sign up for the networkx mailing list and ask your question there.  You are certain to get a faster and more relevant answer. (See the "mailing list" link here: http://networkx.github.io/)

Hope this helps,
Jason




More information about the Python-list mailing list