开发者

Need help with NetworkX

Cu开发者_如何转开发rrently 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


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 )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜