[英]How to specify the <key> id when writing GraphML from NetworkX?
When creating a graph in NetworkX and exporting to GraphML, the keys automatically get turned into "d%i"
.在 NetworkX 中创建图形并导出到 GraphML 时,键会自动变成
"d%i"
。 For example:例如:
import networkx as nx
G = nx.DiGraph() # or DiGraph, MultiGraph, MultiDiGraph, etc
G.add_node(1, labelV="person", age=29, name="marko")
G.add_node(2, labelV="person", age=27, name="vadas")
G.add_node(3, labelV="software", name="lop", lang="java")
G.add_node(4, labelV="person", age=32, name="josh")
G.add_node(5, labelV="software", name="ripple", lang="java")
G.add_node(6, labelV="person", age=35, name="peter")
G.add_edge(1, 2, labelE="knows", weight=1.0)
G.add_edge(1, 4, labelE="knows", weight=1.0)
G.add_edge(1, 3, labelE="created", weight=0.4)
G.add_edge(4, 5, labelE="created", weight=1.0)
G.add_edge(4, 3, labelE="created", weight=0.4)
G.add_edge(6, 3, labelE="created", weight=0.2)
nx.write_graphml(G, "networkx_tinkergraph_modern.xml")
makes something like做类似的东西
<?xml version='1.0' encoding='utf-8'?>
<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="weight" attr.type="double" for="edge" id="d5"/>
<key attr.name="labelE" attr.type="string" for="edge" id="d4"/>
<key attr.name="lang" attr.type="string" for="node" id="d3"/>
<key attr.name="name" attr.type="string" for="node" id="d2"/>
<key attr.name="age" attr.type="long" for="node" id="d1"/>
<key attr.name="labelV" attr.type="string" for="node" id="d0"/>
<graph edgedefault="directed"><node id="1">
<data key="d0">person</data>
<data key="d1">29</data>
<data key="d2">marko</data>
</node>
<node id="2">
<data key="d0">person</data>
<data key="d1">27</data>
<data key="d2">vadas</data>
</node>
<node id="3">
...
I'm looking to make the keys keep the same name as the as attr.name
as specified in the <key>
tag:我希望使键的名称与
<key>
标记中指定的attr.name
保持相同的名称:
<key id="labelV" for="node" attr.name="labelV" attr.type="string"/>
<key id="name" for="node" attr.name="name" attr.type="string"/>
<key id="lang" for="node" attr.name="lang" attr.type="string"/>
<key id="age" for="node" attr.name="age" attr.type="int"/>
<key id="labelE" for="edge" attr.name="labelE" attr.type="string"/>
<key id="weight" for="edge" attr.name="weight" attr.type="double"/>
<graph id="G" edgedefault="directed">
<node id="1">
<data key="labelV">person</data>
<data key="name">marko</data>
<data key="age">29</data>
</node>
<node id="2">
<data key="labelV">person</data>
<data key="name">vadas</data>
<data key="age">27</data>
</node>
<node id="3">
<data key="labelV">software</data>
<data key="name">lop</data>
<data key="lang">java</data>
</node>
...
Someone asked the same question on the Google forums but was told to change how GraphML is read by a Gremlin server, not how it is written using NetworkX.有人在 Google 论坛上问过同样的问题,但被告知要更改 Gremlin 服务器读取 GraphML 的方式,而不是使用 NetworkX 编写它的方式。
Is there a way to make nx.write_graphml()
or nx.generate_graphml()
create files using labelV
, age
, ... instead of d0
, d1
, ... for the id=
in <key>
tags?有没有办法让
nx.write_graphml()
或nx.generate_graphml()
使用labelV
, age
,...而不是d0
, d1
,...为<key>
标签中的id=
创建文件?
In NetworkX 2.5 this is now possible with named_key_ids
:在 NetworkX 2.5 中,现在可以使用
named_key_ids
:
nx.write_graphml(G, "networkx_tinkergraph_modern.xml", named_key_ids=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.