需要从 XML 中提取一些条目。最开始尝试使用一种相当手动的方式来做到这一点,后来有人建议使用 XML 解析器。已经尝试使用 lxml,但无法掌握它 (我刚开始学习 Python)。
XML 结构的一个示例如下 (将会有许多“ProgramInformation”的分支):
<TVAMain xml:lang="NL" publisher="" publicationTime="2013-09-12T01:43:09+00:00" version="217" xmlns="urn:tva:metadata:2010" xmlns:mpeg7="urn:tva:mpeg7:2008" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:tva:metadata:2010>
<ProgramDescription>
<ProgramInformationTable>
<ProgramInformation programId="crid://bds.tv/95291775">
<BasicDescription>
<Title xml:lang="EN" type="main">Rip Off Britain</Title>
<Synopsis xml:lang="EN" length="short">Consumer series. The team investigates why some viewers have been hit with bills they did not expect for hundreds or even thousands of pounds, and offers some advice.</Synopsis>
<Keyword xml:lang="EN" type="main">bills</Keyword>
<Genre href="urn:tva:metadata:cs:UPCEventGenreCS:2009:82">
<Name xml:lang="EN">Economics</Name>
</Genre>
<Language>EN</Language>
</BasicDescription>
</ProgramInformation>
</ProgramInformationTable>
</ProgramDescription>
</TVAMain>
可以使用以下代码检索“crid”和“title”:
import xml.etree.ElementTree as etree
tree = etree.parse('UPC_Medium.xml')
root = tree.getroot()
print (root[0][0][0].attrib)
print (root[0][0][0][0][0].text)
这两个项始终位于每个分支下的相同位置(因此理论上可以用这种相当粗略的方式检索它们)。还需要检索流派,但可能会改变(因为会有数量不定的“关键字”条目)。
认为 XPATH 是关键,但不知道如何实现它(已经查看了很多关于这个的示例)。
请帮助我实现以下目标:
- 实现 XPATH 以检索并将“crid”、“title”和“genre”存储到单独的变量中(将写入外部文件 - 所有三个都需要一起写入)
- 遍历每个分支以提取上述内容 - 将有成千上万个条目。
2、解决方案
import xml.etree.ElementTree as etree
nsmap = {'xmlns': 'urn:tva:metadata:2010'}
with open('UPC_Medium.xml', 'r') as f:
tree = etree.parse(f)
root = tree.getroot()
for info in root.xpath('//xmlns:ProgramInformation', namespaces=nsmap):
crid = info.get('programId')
title = info.find('.//xmlns:Title', namespaces=nsmap).text
genre = info.find('.//xmlns:Genre/xmlns:Name', namespaces=nsmap).text
# 将信息写入外部文件
with open('output.txt', 'a') as out:
out.write(f'{crid}, {title}, {genre}\n')
上述代码使用 ElementTree 库解析 XML 文件并提取所需的信息。通过使用 xpath()
方法,我们可以使用 XPath 表达式在 XML 文档中查找所需的元素。通过 get()
方法可以获取元素的属性值,通过 text
属性可以获取元素的文本内容。
最后,使用 with
语句将信息写入外部文件。with
语句确保在发生错误时文件被正确关闭。