Jon H
Jon H

Reputation: 33

Using edgelist with 3 columns in txt.file

I'm new to NetworkX and I have a problem. I have .txt file containing lots of data in 3 columns separated by tab like this:

1   21   \N
2   61   \N
2   62   1201231
50  11   54432

How can I use read.edgelist('data.txt', create_using=nx.Graph(), nodetype=int) using only first 2 columns?

Upvotes: 0

Views: 793

Answers (1)

abc
abc

Reputation: 11939

A solution which does not use nx.read_edgelist() but the default constructor is as follows.

>>> with open("file.txt") as f:
...     g = nx.Graph([line.split()[:2] for line in f])
... 
>>> g.edges()
EdgeView([('1', '21'), ('2', '61'), ('2', '62'), ('50', '11')])

If the nodetype is required to be integer you can convert to int while reading the file.

For example,

[tuple(map(int,line.split()[:2])) for line in f]

Upvotes: 1

Related Questions