如何在python中从xml文件读取数据

如何在python中从xml文件读取数据,python,xml,lxml,Python,Xml,Lxml,我有以下xml文件数据: <?xml version="1.0" encoding="iso-8859-1" standalone="yes"?> <rootnode> <TExportCarcass> <BodyNum>6168</BodyNum> <BodyWeight>331.40</BodyWeight> <UnitID>1</UnitID> &

我有以下xml文件数据:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<rootnode>
  <TExportCarcass>
    <BodyNum>6168</BodyNum>
    <BodyWeight>331.40</BodyWeight>
    <UnitID>1</UnitID>
    <Plant>239</Plant>
    <pieces>
      <TExportCarcassPiece index="0">
        <Bruising>0</Bruising>
        <RFIDPlant></RFIDPlant>
      </TExportCarcassPiece>
      <TExportCarcassPiece index="1">
        <Bruising>0</Bruising>
        <RFIDPlant></RFIDPlant>
      </TExportCarcassPiece>
    </pieces>
  </TExportCarcass>
  <TExportCarcass>
    <BodyNum>6169</BodyNum>
    <BodyWeight>334.40</BodyWeight>
    <UnitID>1</UnitID>
    <Plant>278</Plant>
    <pieces>
      <TExportCarcassPiece index="0">
        <Bruising>0</Bruising>
        <RFIDPlant></RFIDPlant>
      </TExportCarcassPiece>
      <TExportCarcassPiece index="1">
        <Bruising>0</Bruising>
        <RFIDPlant></RFIDPlant>
      </TExportCarcassPiece>
    </pieces>
  </TExportCarcass>
</rootnode>
但它唯一的印刷品是6168而非None。请说明我做错了什么

只需使用python的内置xml.etree.etree模块即可

您需要迭代每个texportcarcas标记,然后使用find访问BodyNum

例:

输出:


您的文档包含多个BodyNum元素。如果只需要第一个元素,则需要在查询中设置显式限制

使用以下基于xpath查询的灵活方法:

在文本字符串上运行find时,它将仅在根级别搜索元素。您可以在find中使用xpath查询来搜索文档中的任何元素:

要仅获取第一个元素,请执行以下操作: 从lxml导入etree doc=etree.parse'file.xml' memoryElem=doc.find'//BodyNum' memoryElem.text 6168 要获取所有元素,请执行以下操作: [doc.iterfind.//BodyNum'中b的b.text] ['6168', '6169'] 1-使用/指定要提取的元素的树级别

2-使用.text提取元素的名称

doc = etree.parse('file.xml')
memoryElem = doc.find("*/BodyNum") #BodyNum is one level down
print(memoryElem.text)  #Specify you want to extract the name of the element

是否有可能获得TexportCarcas的号码?当然,谢谢,我想我们可以使用评论部分来询问更多信息。@SAndrew,你确定这种方法值得被愚蠢地否决吗?这也是一个有效的答案。不知道为什么会被否决
from lxml import etree

doc = etree.parse('file.xml')
for elem in doc.findall('TExportCarcass'):
    print(elem.find("BodyNum").text) 
6168
6169
print([i.text for i in doc.findall('TExportCarcass/BodyNum')]) #-->['6168', '6169']
from lxml import etree

doc = etree.parse('file.xml')
memoryElem = doc.xpath('(//BodyNum)[1]/text()')
print(memoryElem)   # ['6168']
doc = etree.parse('file.xml')
memoryElem = doc.find("*/BodyNum") #BodyNum is one level down
print(memoryElem.text)  #Specify you want to extract the name of the element