2012-01-30 33 views
5

---更新3: 我有腳本將所需數據更新到已完成的xml文件中,但下面的代碼正在從寫入的文件中刪除。爲什麼是這樣?我怎樣才能取代它?使用python搜索並替換xml /文本文件中的多行

<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type='text/xsl' href='ANZMeta.xsl'?> 

當前工作代碼(上述問題除外)。

import os, xml, arcpy, shutil 
from xml.etree import ElementTree as et 

path=os.getcwd() 
arcpy.env.workspace = path 

FileList = arcpy.ListFeatureClasses() 
FileCount = len(FileList) 
zone="_Zone" 

for File in FileList: 
    FileDesc_obj = arcpy.Describe(File) 
    FileNm=FileDesc_obj.file 
    newMetaFile=FileNm+"_BaseMetadata.xml" 

    check_meta=os.listdir(path) 
    if FileNm+'.xml' in check_meta: 
     shutil.copy2(FileNm+'.xml', newMetaFile) 
    else: 
     shutil.copy2('L:\Data_Admin\QA\Metadata_python_toolset\Master_Metadata.xml', newMetaFile) 
    tree=et.parse(newMetaFile) 

    print "Processing: "+str(File) 

    for node in tree.findall('.//title'): 
     node.text = str(FileNm) 
    for node in tree.findall('.//northbc'): 
     node.text = str(FileDesc_obj.extent.YMax) 
    for node in tree.findall('.//southbc'): 
     node.text = str(FileDesc_obj.extent.YMin) 
    for node in tree.findall('.//westbc'): 
     node.text = str(FileDesc_obj.extent.XMin) 
    for node in tree.findall('.//eastbc'): 
     node.text = str(FileDesc_obj.extent.XMax)   
    for node in tree.findall('.//native/nondig/formname'): 
     node.text = str(os.getcwd()+"\\"+File) 
    for node in tree.findall('.//native/digform/formname'): 
     node.text = str(FileDesc_obj.featureType) 
    for node in tree.findall('.//avlform/nondig/formname'): 
     node.text = str(FileDesc_obj.extension) 
    for node in tree.findall('.//avlform/digform/formname'): 
     node.text = str(float(os.path.getsize(File))/int(1024))+" KB" 
    for node in tree.findall('.//theme'): 
     node.text = str(FileDesc_obj.spatialReference.name +" ; EPSG: "+str(FileDesc_obj.spatialReference.factoryCode)) 
    print node.text 
    projection_info=[] 
    Zone=FileDesc_obj.spatialReference.name 

    if "GCS" in str(FileDesc_obj.spatialReference.name): 
     projection_info=[FileDesc_obj.spatialReference.GCSName, FileDesc_obj.spatialReference.angularUnitName, FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName] 
     print "Geographic Coordinate system" 
    else: 
     projection_info=[FileDesc_obj.spatialReference.datumName, FileDesc_obj.spatialReference.spheroidName, FileDesc_obj.spatialReference.angularUnitName, Zone[Zone.rfind(zone)-3:]] 
     print "Projected Coordinate system" 
    x=0 
    for node in tree.findall('.//spdom'): 
     for node2 in node.findall('.//keyword'): 
      print node2.text 
      node2.text = str(projection_info[x]) 
      print node2.text 
      x=x+1 


    tree.write(newMetaFile) 

---更新1 & 2: 感謝Aleyna我有工作

import os, xml, arcpy, shutil 
from xml.etree import ElementTree as et 

CodeString=['northbc','southbc', '<nondig><formname>'] 

nondig='nondigital' 
path=os.getcwd() 
arcpy.env.workspace = path 
xmlfile = path+"\\test.xml" 

FileList = arcpy.ListFeatureClasses() 
FileCount = len(FileList) 

for File in FileList: 
    FileDesc_obj = arcpy.Describe(File) 
    FileNm=FileDesc_obj.file 
    newMetaFile=FileNm+"_Metadata.xml" 
    shutil.copy2('L:\Data_Admin\QA\Metadata_python_toolset\Master_Metadata.xml', newMetaFile) 
    tree=et.parse(newMetaFile) 

    for node in tree.findall('.//northbc'): 
     node.text = str(FileDesc_obj.extent.YMax) 
    for node in tree.findall('.//southbc'): 
     node.text = str(FileDesc_obj.extent.YMin) 
    for node in tree.findall('.//westbc'): 
     node.text = str(FileDesc_obj.extent.XMin) 
    for node in tree.findall('.//eastbc'): 
     node.text = str(FileDesc_obj.extent.XMax)   
    for node in tree.findall('.//native/nondig/formname'): 
     node.text = nondig 

    tree.write(newMetaFile) 

問題以下基本代碼是用XML代碼打交道就像

- <spdom> 
    <keyword thesaurus="">GDA94</keyword> 
    <keyword thesaurus="">GRS80</keyword> 
    <keyword thesaurus="">Transverse Mercator</keyword> 
    <keyword thesaurus="">Zone 55 (144E - 150E)</keyword> 
    </spdom> 

由於關鍵字...在<spdom>內不是唯一的,我們可以按照來自

FileDesc_obj.spatialReference.name 

u'GCS_GDA_1994'

---原來的職位---

我建立了一個項目來產生我們的圖書館從空間文件的XML元數據文件。我已經創建了腳本以從文件中提取所需的空間和屬性數據,並創建基於shp和文本文件的文件索引,但現在我想將此信息寫入基本元數據xml文件,該文件通過將文件寫入anzlic標準由普通/靜態元素保存的值...

因此,例如,我想與

<northbc> GeneratedValue_[desc.extent.XMax] /<northbc> 
<southbc> GeneratedValue_[desc.extent.XMax] </southbc> 

問題,以取代以下XML代碼

<northbc>8097970</northbc> 
<southbc>8078568</southbc> 

的是,明明數/值之間和將不會是相同的。

類似的xml標籤,如<title>, <nondig><formname> etc ...在後面的例子中,兩個標籤必須一起搜索,因爲formname多次出現(不是唯一的)。

我使用Python的正則表達式手冊[這裏] [1],

+1

請參閱http://stackoverflow.com/a/1732454/383402 – Borealid 2012-01-30 03:02:46

+0

謝謝...我不想從頭開始編寫一個xml文件。我只想根據來自arcpy模塊的輸入來替換給定屬性中的文本塊。 – GeorgeC 2012-01-30 03:21:37

+1

因此,當它產生看起來像'<! - Comment - > 8097970'的輸出時,你的正則表達式會處理它嗎? – Borealid 2012-01-30 03:22:48

回答

2

使用上面給定的標籤:

import os 
import xml 
from xml.etree import ElementTree as et 
path = r"/your/path/to/xml.file" 
tree = et.parse(path) 
for node in tree.findall('.//northbc'): 
    node.text = "New Value" 
tree.write(path) 

這裏,XPATH。//northbc返回XML文檔中的所有'northbc'節點。您可以輕鬆地爲您的需求量身定製代碼。

+0

謝謝,但我得到以下... >> path = os.getcwd() >> tree = et.parse(path) Traceback(最近調用最後一個): 文件「C:\ Program Files(x86)\ Wing IDE 101 4.0 \ src \ debug \ tserver \ _sandbox.py」,第1行,在 #內部用於外部解釋器下的調試沙箱 解析文件「C:\ Python26 \ ArcGIS10.0 \ Lib \ xml \ etree \ ElementTree.py」,第862行 tree.parse(source,parser) 文件「C: \ Python26 \ ArcGIS10.0 \ Lib \ xml \ etree \ ElementTree.py「,第579行,解析爲 source = open(source,」rb「) IOError:[ Errno 13] Permission denied:'L:\\ Data_Admin \\ QA \\ Metadata_python_toolset \\ training' – GeorgeC 2012-01-30 05:19:49

+0

請忽略我以前的評論。當path是一個實際的xml文件時,它工作正常。如何重複標籤,如第三個示例 - '',其中formname重複但nondig是唯一的。 – GeorgeC 2012-01-30 05:26:30

+0

如果我正確地做到了,您有多個 s,它們是獨特的節點的直接子女?然後你可以使用這樣一個xpath。//nondig/formname得到 s。您可以在樹中查找並在更換值之前檢查父項,或者甚至更好地使用父級的唯一屬性(可能是id?)重寫xpath,以便 s將按 s分組。 – Aleyna 2012-01-30 05:54:52

0

我可能會在這裏說明明顯,但你考慮使用DOM樹來解析和處理您的XML?

1

如果您正在處理有效的XML,請使用XPath查找感興趣的節點以及ElementTree API來操作節點。

例如,你的xpath可能類似'// northbc',你只需要替換裏面的文本節點。

請參閱http://docs.python.org/library/xml.etree.elementtree.html以及http://pypi.python.org/pypi/lxml/2.2.8兩個不同的庫,這將幫助您完成此操作。搜索谷歌的XPath和看到一個體面的XPath介紹w3c教程(我顯然不能發佈超過兩個鏈接的帖子或我也鏈接)

+0

謝謝。這似乎是在正確的軌道上,我剛剛通過http://www.w3schools.com/xpath/ – GeorgeC 2012-01-30 03:42:39

相關問題