1

I am having issues trying to read in the following data from a file into NetworkX to build a graph.

Example Data:

The pipe '|' separates the source node (user) from the destination nodes (users). As an example entry:

s1|d1,d2,d3

means that there is an edge between user s1 and user d1, and there is also an edge between user s1 and user d2, etc.

1 Answer 1

1

You can use str.split() to split the lines and parse accordingly. Example:

G = nx.Graph()
with open('g.txt') as f:
   for line in f:
      u, destinations = line.split('|')
         for v in destinations.split(','):
            G.add_edge(u,v)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.