ooozooo
ooozooo

Reputation: 167

adding edges by using networkx functions

I'm trouble with making graph by networkx. I'm trying to add edges but my code doesn't work. I followed the way suggested by tutorial from http://networkx.lanl.gov/.

Is there any help on my code? G.nodes() and G.edges() didn't print out anything.

import networkx as nx

fh = open("internal_link.txt", 'rb')
G = nx.parse_edgelist(fh, delimiter='\t' and '\n', create_using=nx.DiGraph())
G.nodes()
G.edges()

My data(internal_link.txt) format is as follows: (the dots means more lines that are exactly same form with previous things)

9991942 15683276
9991942 15763818
9991942 15764086
9991942 15769010
9991942 15771971
9991942 15778227
9991942 15784302
9991942 15793787
9991942 15811314
9991942 15823044
9991942 15843518
9991942 15865514
9991942 15874410
9991942 15936166
9991942 15959560
9991942 16027570
9991942 16043690
9991942 16049723
.
.
.
.

Upvotes: 1

Views: 513

Answers (1)

Michael J. Barber
Michael J. Barber

Reputation: 25042

By using delimiter='\t' and '\n', you are telling NetworkX that you want the lines from fh split on the result of '\t' and '\n', which is just '\n'. Since '\n' is used to separate lines, no line will contain '\n' and there will thus be only one token per line, giving no valid edges.

The sample data shows spaces separating the nodes. It's not really possible to tell whether that means the file uses spaces, since Stackoverflow doesn't really handle tabs very well. You don't need to specify the delimiter at all, though, just let it use the default whitespace.

Your script then becomes:

fh = open("internal_link.txt", 'rb')
G = nx.parse_edgelist(fh, create_using=nx.DiGraph())

However, you might as well just use the simpler read_edgelist:

G = nx.read_edgelist("internal_link.txt", create_using=nx.DiGraph())

That should produce a valid graph (and does when I try it with the portion of the data file you've shown).

Note that neither G.nodes() nor G.edges() actually prints anything. If you're working interactively in the Python interpreter, you'll see the results, but you'll need to be more explicit in a script:

print G.nodes()
print G.edges()

Upvotes: 2

Related Questions