如何从 python 中的 xml 文件中读取数据
How to read data from xml file in python
我有以下 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>
我正在使用 python 的 lxml
模块从 xml 文件中读取数据,如下所示:
from lxml import etree
doc = etree.parse('file.xml')
memoryElem = doc.find('BodyNum')
print(memoryElem)
但它只打印 None
而不是 6168
。请建议我在这里做错了什么。
只需使用 python
的内置 xml.etree.Etree
模块
https://docs.python.org/3/library/xml.etree.elementtree.html
您需要迭代每个 TExportCarcass
标签,然后使用 find
访问 BodyNum
例如:
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']
您的文档包含多个 BodyNum
元素。
如果您只需要第一个元素,则需要在查询中明确限制。
根据 xpath
查询使用以下灵活方法:
from lxml import etree
doc = etree.parse('file.xml')
memoryElem = doc.xpath('(//BodyNum)[1]/text()')
print(memoryElem) # ['6168']
当您 运行 find
文本字符串时,它只会搜索根级别的元素。您可以改为使用 find
中的 xpath
查询来搜索文档中的任何元素:
- 只获取第一个元素:
from lxml import etree
doc = etree.parse('file.xml')
memoryElem = doc.find('.//BodyNum')
memoryElem.text
# 6168
- 获取所有元素:
[ b.text for b in doc.iterfind('.//BodyNum') ]
# ['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
我有以下 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>
我正在使用 python 的 lxml
模块从 xml 文件中读取数据,如下所示:
from lxml import etree
doc = etree.parse('file.xml')
memoryElem = doc.find('BodyNum')
print(memoryElem)
但它只打印 None
而不是 6168
。请建议我在这里做错了什么。
只需使用 python
的内置xml.etree.Etree
模块
https://docs.python.org/3/library/xml.etree.elementtree.html
您需要迭代每个 TExportCarcass
标签,然后使用 find
访问 BodyNum
例如:
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']
您的文档包含多个 BodyNum
元素。
如果您只需要第一个元素,则需要在查询中明确限制。
根据 xpath
查询使用以下灵活方法:
from lxml import etree
doc = etree.parse('file.xml')
memoryElem = doc.xpath('(//BodyNum)[1]/text()')
print(memoryElem) # ['6168']
当您 运行 find
文本字符串时,它只会搜索根级别的元素。您可以改为使用 find
中的 xpath
查询来搜索文档中的任何元素:
- 只获取第一个元素:
from lxml import etree
doc = etree.parse('file.xml')
memoryElem = doc.find('.//BodyNum')
memoryElem.text
# 6168
- 获取所有元素:
[ b.text for b in doc.iterfind('.//BodyNum') ]
# ['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