2011-11-23 83 views
1

我是Python新手。現在我必須用Python替換XML文件中的一些值。 XML的例子片段是:如何用Python替換XML中的節點值

<gmd:extent> 
    <gmd:EX_Extent> 
     <gmd:description gco:nilReason="missing"> 
     <gco:CharacterString /> 
     </gmd:description> 
     <gmd:geographicElement> 
     <gmd:EX_GeographicBoundingBox> 
      <gmd:westBoundLongitude> 
      <gco:Decimal>112.907</gco:Decimal> 
      </gmd:westBoundLongitude> 
      <gmd:eastBoundLongitude> 
      <gco:Decimal>158.96</gco:Decimal> 
      </gmd:eastBoundLongitude> 
      <gmd:southBoundLatitude> 
      <gco:Decimal>-54.7539</gco:Decimal> 
      </gmd:southBoundLatitude> 
      <gmd:northBoundLatitude> 
      <gco:Decimal>-10.1357</gco:Decimal> 
      </gmd:northBoundLatitude> 
     </gmd:EX_GeographicBoundingBox> 
     </gmd:geographicElement> 
    </gmd:EX_Extent> 
    </gmd:extent> 

我想要做的是,以取代那些十進制值,即112.907,用指定的值。

<gmd:extent> 
    <gmd:EX_Extent> 
     <gmd:description gco:nilReason="missing"> 
     <gco:CharacterString /> 
     </gmd:description> 
     <gmd:geographicElement> 
     <gmd:EX_GeographicBoundingBox> 
      <gmd:westBoundLongitude> 
      <gco:Decimal>new value</gco:Decimal> 
      </gmd:westBoundLongitude> 
      <gmd:eastBoundLongitude> 
      <gco:Decimal>new value</gco:Decimal> 
      </gmd:eastBoundLongitude> 
      <gmd:southBoundLatitude> 
      <gco:Decimal>new value</gco:Decimal> 
      </gmd:southBoundLatitude> 
      <gmd:northBoundLatitude> 
      <gco:Decimal>new value</gco:Decimal> 
      </gmd:northBoundLatitude> 
     </gmd:EX_GeographicBoundingBox> 
     </gmd:geographicElement> 
    </gmd:EX_Extent> 
    </gmd:extent> 

我試着用一些方法,但沒有人與我的假設是難度與命名空間前綴GMD和GCO工作。

請幫我一把。提前致謝!

乾杯,亞歷克斯

+1

哪個XML解析器? – chown

回答

3

我不能讓LXML來處理XML,而不在頂部添加假命名空間聲明所以這裏是你輸入的樣子

<gmd:extent xmlns:gmd="urn:x:y:z:1" xmlns:gco="urn:x:y:z:1"> 
    <gmd:EX_Extent> 
     <gmd:description gco:nilReason="missing"> 
      <gco:CharacterString /> 
     </gmd:description> 
     <gmd:geographicElement> 
      <gmd:EX_GeographicBoundingBox> 
       <gmd:westBoundLongitude> 
        <gco:Decimal>112.907</gco:Decimal> 
       </gmd:westBoundLongitude> 
       <gmd:eastBoundLongitude> 
        <gco:Decimal>158.96</gco:Decimal> 
       </gmd:eastBoundLongitude> 
       <gmd:southBoundLatitude> 
        <gco:Decimal>-54.7539</gco:Decimal> 
       </gmd:southBoundLatitude> 
       <gmd:northBoundLatitude> 
        <gco:Decimal>-10.1357</gco:Decimal> 
       </gmd:northBoundLatitude> 
      </gmd:EX_GeographicBoundingBox> 
     </gmd:geographicElement> 
    </gmd:EX_Extent> 
</gmd:extent> 

我假設你有兩個列表一個爲當前值和一個用於新的這樣

老= [112.907,158.96,-54.7539,-10.1357] 新= [1,2,3,4] d =字典(拉鍊(舊,新))

下面是完整的代碼

#!/usr/bin/env python 
import sys 
from lxml import etree 

def process(fname): 
    f = open(fname) 
    tree = etree.parse(f) 
    root = tree.getroot() 
    old = [112.907, 158.96, -54.7539, -10.1357] 
    new = [1,2,3,4] 
    d = dict(zip(old,new)) 
    nodes = root.findall('.//gco:Decimal', root.nsmap) 
    for node in nodes: 
     node.text = str(d[float(node.text)]) 
    f.close() 
    return etree.tostring(root, pretty_print=True) 

def main(): 
    fname = sys.argv[1] 
    text = process(fname) 
    outfile = open('out.xml', 'w+') 
    outfile.write(text) 
    outfile.close() 

if __name__ == '__main__': 
    main() 

,這裏是您使用的輸出如何看起來像

<gmd:extent xmlns:gmd="urn:x:y:z:1" xmlns:gco="urn:x:y:z:1"> 
    <gmd:EX_Extent> 
     <gmd:description gco:nilReason="missing"> 
      <gco:CharacterString/> 
     </gmd:description> 
     <gmd:geographicElement> 
      <gmd:EX_GeographicBoundingBox> 
       <gmd:westBoundLongitude> 
        <gco:Decimal>1</gco:Decimal> 
       </gmd:westBoundLongitude> 
       <gmd:eastBoundLongitude> 
        <gco:Decimal>2</gco:Decimal> 
       </gmd:eastBoundLongitude> 
       <gmd:southBoundLatitude> 
        <gco:Decimal>3</gco:Decimal> 
       </gmd:southBoundLatitude> 
       <gmd:northBoundLatitude> 
        <gco:Decimal>4</gco:Decimal> 
       </gmd:northBoundLatitude> 
      </gmd:EX_GeographicBoundingBox> 
     </gmd:geographicElement> 
    </gmd:EX_Extent> 
</gmd:extent> 
+0

+1,因爲它甚至可以嘗試在這裏http://stackoverflow.com/a/15396987/603855嘗試VBScript(?恐怖?)解決方案。 –