2012-01-30 81 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