Need help with NetworkX

2019-07-24 23:27发布

Currently im faced with the following problem:

I have a script that searches through a specific directory that contains documents. Each document is assigned a number within the filename. Within each document are numbers that also represent another document (filename). How can I create a web that shows what documents lead to what?

Any help would be appreciated, thanks

1条回答
Explosion°爆炸
2楼-- · 2019-07-25 00:09

This is a textbook example of a directed graph. You should read the NetworkX tutorial to get a better idea of how to work with them; basically, you need to add all the nodes (points), in this case file numbers, and then add edges between them.

import os
import networkx as nx

g = nx.DiGraph( )
for filename in os.listdir( <dir> ):
    # do something to filename to get the number
    g.add_node( <number> )

for filename in os.listdir( <dir> ):
    # do something to filename to get the source
    with open( filename ) as theFile:
        # do something to theFile to get the targets
        for target in <targets>:
            g.add_edge( <source>, <target> )

import matplotlib.pyplot as plt
nx.draw( g )
查看更多
登录 后发表回答