1

私はnetworkxを非常に表面的に使用しています。グラフを graphml のようにファイルに書き込むのは簡単ですが、ファイル システムを気にせずにグラフを文字列に保存するにはどうすればよいでしょうか?

そのドキュメントはそれが可能であると言います。

4

2 に答える 2

8

ほとんどの形式には「ジェネレーター」もあります。したがって、StringIO を使用せずにこの方法で実行できます。

In [1]: import networkx as nx

In [2]: G=nx.path_graph(4)

In [3]: s='\n'.join(nx.generate_graphml(G))

In [4]: print s
<graphml xmlns="http://graphml.graphdrawing.org/xmlns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key attr.name="name" attr.type="string" for="graph" id="d0" />
  <graph edgedefault="undirected">
    <data key="d0">path_graph(4)</data>
    <node id="0" />
    <node id="1" />
    <node id="2" />
    <node id="3" />
    <edge source="0" target="1" />
    <edge source="1" target="2" />
    <edge source="2" target="3" />
  </graph>
</graphml>
于 2012-06-15T19:28:07.120 に答える
5

larsmans がコメントしたように、以下を使用して可能StringIOです:

import networkx as nx
import StringIO
import itertools

g = nx.Graph()

edges = itertools.combinations([1,2,3,4], 2)
g.add_edges_from(edges)

# File-like object
output = StringIO.StringIO()

nx.write_graphml(g, output)

# And here's your string
gstr = output.getvalue()
print gstr
output.close()
于 2012-06-15T12:10:20.720 に答える