将xml中的<*></*>标记中的数据转储到python中的csv(多种不同的xml样式表格式)

2024-10-02 00:28:49 发布

您现在位置:Python中文网/ 问答频道 /正文

--即使在了解了一些XSLT之后,我也没有使用它,因为元数据/xls格式发生了变化,因此单一的基于样式表的方法将无法工作--

在过去的几个小时里,我一直在尝试获取一个csv并将每个标签中的数据转储到csv中,但是没有任何效果。我尝试过elemtree、parse和regex,这些都是基于论坛中其他几个问题和解答的。在

For example对他的测试数据很好,但对我的xml(问题末尾的示例)不起作用。在

tree = ET.parse("test2.xml")
doc = tree.getroot()
thingy = doc.find('custod')
print thingy.attrib

回溯(最近调用最后一次):文件“”,行 1,在AttributeError中:“NoneType”对象没有属性 '属性'

^{pr2}$

回溯(最近一次调用):文件“”,第1行,在AttributeError中:“NoneType”对象没有属性“attrib”

doc.attrib
{}

---尝试使用REX

rex = re.compile(r'<custod.*?>(.*?)</custod>',re.S|re.M)
rex
<_sre.SRE_Pattern object at 0x080724A0>
match=rex.match('test2.xml')
match
text = match.groups()[0].strip()

回溯(最近一次呼叫): 文件“”,第1行,输入 AttributeError:“NoneType”对象没有属性“groups”


我所需要的就是让系统浏览我的xml文件并创建一个csv,其中包含csv列中每个标记的完整条目。如果csv不存在,则添加相应的列。在

在=========== XML示例

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='ANZMeta.xsl'?>
<anzmeta>
  <citeinfo>
    <uniqueid />
    <title>&lt;&gt;</title>
    <origin>
      <custod>ATGIS</custod>
      <jurisdic>
        <keyword thesaurus="">Tablelands Regional Council</keyword>
      </jurisdic>
    </origin>
  </citeinfo>
  <descript>
    <abstract>&lt;&gt;
    </abstract>
    <theme>
      <keyword thesaurus="">EPSG</keyword>
    </theme>
    <spdom>
      <keyword thesaurus="">GDA94</keyword>
      <keyword thesaurus="">GRS80</keyword>
      <keyword thesaurus="">Map Grid of Australia</keyword>
      <keyword thesaurus="">Zone 55 (144E - 150E)</keyword>
      <bounding>
        <northbc />
        <southbc />
        <eastbc />
        <westbc />
      </bounding>
    </spdom>
  </descript>
  <timeperd>
    <begdate>
      <date>2012</date>
    </begdate>
    <enddate>
      <keyword thesaurus="">Completed</keyword>
    </enddate>
  </timeperd>
  <status>
    <progress>
      <keyword thesaurus="">Ongoing</keyword>
      <keyword thesaurus="">Completed</keyword>
    </progress>
    <update>
      <keyword thesaurus="">As Required</keyword>
      <keyword thesaurus="">As Required</keyword>
    </update>
  </status>
  <distinfo>
    <native>
      <nondig>
        <formname>File</formname>
      </nondig>
      <digform>
        <formname>Type:</formname>
      </digform>
    </native>
    <avlform>
      <nondig>
        <formname>Format:</formname>
      </nondig>
      <digform>
        <formname>Size</formname>
      </digform>
    </avlform>
    <accconst>Internal Use Only</accconst>
  </distinfo>
  <dataqual>
    <lineage>~TBC~</lineage>
    <procstep>
      <procdesc Sync="TUE">Metadata imported.</procdesc>
      <srcused Sync="TRUE">L:\Data_Admin\MetadataGenerator\trc_Metadata_Template.xml</srcused>
      <date Sync="TRUE">20121206</date>
      <time Sync="TRUE">15341400</time>
    </procstep>
    <posacc>~TBC~</posacc>
    <attracc>~TBC~</attracc>
    <logic>~TBC~</logic>
    <complete>~TBC~</complete>
  </dataqual>
  <cntinfo>
    <cntorg>Atherton Tablelands GIS</cntorg>
    <cntpos>GIS Coordinator</cntpos>
    <address>PO Box 1616, 8 Tolga Rd</address>
    <city>Atherton</city>
    <state>QLD</state>
    <country>AUSTRALIA</country>
    <postal>4883</postal>
    <cntvoice>07 40918600</cntvoice>
    <cntfax>07 40917035</cntfax>
    <cntemail>info@atgis.com.au</cntemail>
  </cntinfo>
  <metainfo>
    <metd>
      <date />
    </metd>
  </metainfo>
</anzmeta>

---开始我的脚本

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

SourceDIR=os.getcwd()
outDIR=os.getcwd()+'//out'

def locatexml(SourceDIR,outDIR):
    xmllist=[]
    for root, dirs, files in os.walk(SourceDIR, topdown=False):
        for fl in files:
            currentFile=os.path.join(root, fl)
            ext=fl[fl.rfind('.')+1:]
            if ext=='xml':
                xmllist.append(currentFile)
                print currentFile
                readxml(currentFile)
    print "finished"
    return xmllist

def readxml(currentFile):
    tree=et.parse(currentFile)
    print "Processing: "+str(currentFile)

locatexml(SourceDIR,outDIR)
print xmllist

Tags: 文件csvdate属性osmatchxmlkeyword
2条回答

<anzmeta>是文档的根,因此您应该尝试find它的一个直接子级(比如citeinfo),而不是根标记名本身。在

实际上,您应该使用XSLT来完成这项工作,因为它是XML到另一种格式的转换。有关示例,请参见this question的答案。在

{不过,如果你想用其他代码来做的话,{

from lxml import etree

with open('test.xml') as f:
    tree = etree.parse(f)

# At this point, we can step through the xml file
# and parse it, here is an example of the `cntinfo` tag

for element in tree.iter('cntinfo'):
    for child in element.getchildren():
        print "{0.tag}: {0.text}".format(child)

这将打印:

^{pr2}$

同样,您也可以逐步检查文件中的其他元素;但我强烈建议您使用XSLT。


此代码段将使用xslt样式表(从this question)将xml文档转换为csv:

# First, we load the stylesheet
with open(r'd:\test.xsl') as f:
    temp = etree.parse(f)
    style_sheet = etree.XSLT(temp)

# Apply it to the previously parsed document tree:
converted_xml = style_sheet(tree)

# Print the results:
str(converted_xml)

这将为您提供:

'"",    "<>",    "ATGISTablelands Regional Council"\r"<>",    "EPSG",
  "GDA94GRS80Map Grid of AustraliaZone 55 (144E - 150E)"\r"2012",    "Completed"
\r"OngoingCompleted",    "As RequiredAs Required"\r"FileType:",    "Format:Size"
,    "Internal Use Only"\r"~TBC~",    "Metadata imported.L:\\Data_Admin\\Metadat
aGenerator\\trc_Metadata_Template.xml2012120615341400",    "~TBC~",    "~TBC~",
   "~TBC~",    "~TBC~"\r"Atherton Tablelands GIS",    "GIS Coordinator",    "PO
Box 1616, 8 Tolga Rd",    "Atherton",    "QLD",    "AUSTRALIA",    "4883",    "0
7 40918600",    "07 40917035",    "info@atgis.com.au"\r""\r'

相关问题 更多 >

    热门问题