使用ElementTree的iter()不带参数解析XML不会返回文件中的前几个标记

2024-10-01 17:21:44 发布

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

我试图从一个XML文件中提取所有标题,并将它们放入python中的列表中,但是,每次运行代码时,从文件中提取的第一个标记实际上并不是XML文件中的第一个标记。而是从第18个标记开始,然后从那里打印列表的其余部分。真正奇怪的是,当我最初编写这段代码时,它按预期工作,但当我添加代码以提取元素文本并将其放入列表时,头代码停止工作,无论是在原始程序中还是在下面的独立代码中。我还应该提到,完整的程序不会以任何方式操作XML文件。在提取之后,所有操作都只在python列表上完成

import xml.etree.ElementTree as ET

tree = ET.parse("Sample.xml")
root = tree.getroot()

headers = [elem.tag for elem in root.iter()]

print(headers)

XML是一个敏感文件,因此我必须对所有元素文本进行编辑。它也是一个非常大的文件,所以我只包含了一个帐户的元素

-<ExternalCollection xmlns="namespace.xsd">
    -<Batch>
        <BatchID>***</BatchID>
        <ExternalCollectorName>***</ExternalCollectorName>
        <PrintDate>***</PrintDate>
        <ProviderOrganization>***</ProviderOrganization>
        <ProvOrgID>***</ProvOrgID>
       -<Account>
           <AccountNum>***</AccountNum>
           <Guarantor>***</Guarantor>
           <GuarantorAddress1>***</GuarantorAddress1>
           <GuarantorAddress2/>
           <GuarantorCityStateZip>***</GuarantorCityStateZip>
           <GuarantorEmail/>
           <GuarantorPhone>***</GuarantorPhone>
           <GuarantorMobile/>
           <GuarantorDOB>***</GuarantorDOB>    
           <AccountID>***</AccountID>
           <GuarantorID>***</GuarantorID>
          -<Incident>
               <Patient>***</Patient>
               <PatientDOB>***</PatientDOB>
               <FacilityName>***</FacilityName>
              -<ServiceLine>
                  <DOS>***</DOS>
                  <Provider>***</Provider>
                  <Code>***</Code>
                  <Modifier>***</Modifier>
                  <Description>***</Description>
                  <Billed>***</Billed>
                  <Expected>***</Expected>
                  <Balance>***</Balance>
                  <SelfPay>***</SelfPay>
                  <IncidentID>***</IncidentID>
                  <ServiceLineID>***</ServiceLineID>
                 -<OtherActivity>  
                  </OtherActivity>
              </ServiceLine>
          </Incident>
      </Account>
  </Batch>
  </ExternalCollection>

结果如下:

 'namespace.xsd}PatientDOB', '{namespace.xsd}FacilityName', '{namespace.xsd}ServiceLine', '{namespace.xsd}DOS', '{namespace.xsd}Provider', '{namespace.xsd}Code', '{namespace.xsd}Modifier', '{namespace.xsd}Description', '{namespace.xsd}Billed', '{namespace.xsd}Expected', '{namespace.xsd}Balance', '{namespace.xsd}SelfPay', '{namespace.xsd}IncidentID', '{namespace.xsd}ServiceLineID', '{namespace.xsd}OtherActivity'

如您所见,出于某种原因,第一个返回值是Patient DOB,而不是实际的第一个标记

提前谢谢大家


Tags: 文件代码标记元素列表codexmlprovider
1条回答
网友
1楼 · 发布于 2024-10-01 17:21:44

输入文件的XML标记前面不应包含“-”字符。 您应该在根标记前面至少放置第一个“-”,否则 出现分析错误

还要注意,您的第一个打印的标记名没有首字母“{”,所以很明显 大概在你的循环之后,你的列表中发生了一些奇怪的事情

我运行了您的代码,得到了一个正确的列表,其中包含所有标记

尝试以下循环:

for elem in root.iter():
    print(elem.tag)

也许它会给你一些关于错误真正原因的线索

考虑升级你的 python 安装。也许你有 一些过时的模块

还有一个提示:只在您包含的输入上运行代码 在您的帖子中,内容替换为“***”。可能是真正的原因 任何源元素的实际内容中都存在错误 (此处用星号替换)

相关问题 更多 >

    热门问题