2017-01-19 13 views
1

こんにちは私は単純なgraphMLファイルを持っています。ノードタグをGraphMLから削除し、別のGraphMLファイルに保存したいと思います。 GraphMLのサイズはサンプルが3GB以下です。GraphMLファイルを別のものに変換する

入力ファイル:

<?xml version="1.0" ?> 
<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.1/graphml.xsd"> 
    <key id="weight" for="edge" attr.name="weight" attr.type="string"></key> 
    <graph id="G" edgedefault="directed"> 
     <node id="1"></node> 
     <node id="2"> 
     </node> 
     <node id="3"> 
     </node> 
     <node id="4"> 
     </node> 
     <node id="5"> 
     </node> 
     <edge id="6" source="1" target="2"> 
      <data key="weight">3</data> 
     </edge> 
     <edge id="7" source="2" target="4"> 
      <data key="weight">1</data> 
     </edge> 
     <edge id="8" source="2" target="3"> 
      <data key="weight">9</data> 
     </edge> 
    </graph> 
</graphml> 

必要な出力:

<?xml version="1.0" ?> 
<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.1/graphml.xsd"> 
    <key id="weight" for="edge" attr.name="weight" attr.type="string"></key> 
    <graph id="G" edgedefault="directed"> 
     <edge id="6" source="1" target="2"> 
      <data key="weight">3</data> 
     </edge> 
     <edge id="7" source="2" target="4"> 
      <data key="weight">1</data> 
     </edge> 
     <edge id="8" source="2" target="3"> 
      <data key="weight">9</data> 
     </edge> 
    </graph> 
</graphml> 

は、これを行うための任意の方法はありますか?

答えて

1

graphmlを扱うPythonモジュールがあります。 奇妙なことに、documentationにはremoveまたはdeleteの機能がありません。

graphmlはxmlマークアップなので、代わりにxmlモジュールを使用できます。 私はxmltodictを使用しており、非常に気に入っています。 このモジュールでは、xmlコードをPythonオブジェクトに読み込むことができます。オブジェクトを変更した後、オブジェクトをxmlに保存することができます。

dataは、XMLを含む文字列である場合:

data_object=xmltodict.parse(data) 
del data_object["graphml"]["graph"]["node"] 
xmltodict.unparse(data_object, pretty=True) 

これはnodeエントリを削除し、unparseは、XMLでの文字列を返します。

xmlの構造が複雑になる場合は、data_objectのノードを検索する必要があります。しかし、それは問題ではありません、それはちょうど順序付けられた辞書です。

もう1つの問題はxmlのサイズかもしれません。 3GBはたくさんあります。 xmltodictは大きなファイルのストリーミングモードをサポートしていますが、それは一度も使用していないものです。

+0

実は、問題はファイルサイズです。私は[xml.etree.ElementTree](https://docs.python.org/3.4/library/xml.etree.elementtree.html#module-xml.etree.ElementTree)のpythonライブラリを使って同じタスクを実行しました。 – arjun045

0

いくつかのリンクを読んだ後、私は反復解析の解決策を思いつきました。 Bt私は単純な解析とiterparseの違いをRAMの使用量で把握することはできません。

重要リンク:
- http://www.ibm.com/developerworks/xml/library/x-hiperfparse/
- using lxml and iterparse() to parse a big (+- 1Gb) XML file

コード:

輸入lxml.etreeらとしては

graphml = { 
    "graph": "{http://graphml.graphdrawing.org/xmlns}graph", 
    "node": "{http://graphml.graphdrawing.org/xmlns}node", 
    "edge": "{http://graphml.graphdrawing.org/xmlns}edge", 
    "data": "{http://graphml.graphdrawing.org/xmlns}data", 
    "weight": "{http://graphml.graphdrawing.org/xmlns}data[@key='weight']", 
    "edgeid": "{http://graphml.graphdrawing.org/xmlns}data[@key='edgeid']" 
} 



for event, elem in et.iterparse("/data/sample.graphml",tag=graphml.get("edge"), events = ('end',)): 
    print(et.tostring(elem)) 
    elem.clear() 
    while elem.getprevious() is not None: 
     del elem.getparent()[0] 
関連する問題