2017-01-19 12 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을 처리 할 파이썬 모듈이 있습니다. 흥미롭게도 documentation에는 remove 또는 delete 기능이 없습니다.

graphml은 xml 마크 업이므로 xml 모듈을 대신 사용할 수 있습니다. 나는 xmltodict을 사용했고 매우 좋아했습니다. 이 모듈을 사용하면 XML 코드를 Python 객체에로드 할 수 있습니다. 객체를 수정 한 후 객체를 xml에 다시 저장할 수 있습니다.

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

이것은 node 항목을 제거에서, unparse는 XML을 문자열을 반환합니다 data 만약

는 XML을 포함하는 문자열입니다.

xml 구조가 복잡 해지면 data_object에서 노드를 검색해야합니다. 그러나 그것은 문제가되어서는 안됩니다.

xml의 크기가 다른 문제 일 수 있습니다. 3GB가 많이 있습니다. xmltodict는 대용량 파일을위한 스트리밍 모드를 지원하지만, 사용하지 않은 것입니다.

+0

사실, 문제는 파일 크기입니다. 나는 [xml.etree.ElementTree] (https://docs.python.org/3.4/library/xml.etree.elementtree.html#module-xml.etree.ElementTree) 파이썬 라이브러리를 사용하여 동일한 작업을 수행했다. – arjun045

0

일부 링크를 읽은 후 반복적 인 구문 분석의 해결책을 생각해 냈습니다. Bt RAM 사용량 측면에서 간단한 구문 분석과 iterparse의 차이점을 이해할 수 없습니다.

중요 링크 :
- 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]