我在解析xml文件时遇到了麻烦,我想寻求一些帮助。
我想要的是基于locale="EN-US"为Synopsis和Title解析一个子元素。
预期的结果将是:
(US)
我可以访问for循环中的两个子元素,或者通过切片根元素.还将子元素存储在列表中,然后从列表中访问描述和标题。在第一次迭代之后,还中断了for循环,这对于Synopsis是有效的,但它还远远不够优雅。
如果有人能帮忙,我会非常感激的。
<VODMetadata>
<General assetID="XYZ" WarnerID="WarnerID12345" assetName="Vertigo" version="1" provider="Warner" providerID="www.warner.com" programmeType="Movie"/>
<MovieInfo>
<Movie id="1159775" name="Vertigo">
<ProductionYear>1958</ProductionYear>
<Studio>Warner</Studio>
<Titles>
<Title locale="ES-AR" type="EPG">Vertigo(ESP)</Title>
<Title locale="EN-US" type="EPG">Vertigo (US)</Title>
</Titles>
<Synopses>
<Synopsis locale="EN-US">
<Short/>
<Medium/>
<Long>Description text english</Long>
</Synopsis>
<Synopsis locale="ES-AR">
<Short/>
<Medium/>
<Long>Description text spanish</Long>
</Synopsis>
</Synopses>
<Genres>
<Genre sequence="1">Drama</Genre>
<Genre sequence="2">Thriller</Genre>
<Genre sequence="3">Mystery</Genre>
</Genres>
</MovieInfo>
</VODMetadata>发布于 2020-10-25 10:12:00
下面
import xml.etree.ElementTree as ET
xml = ''' <VODMetadata>
<General assetID="XYZ" WarnerID="WarnerID12345" assetName="Vertigo" version="1" provider="Warner" providerID="www.warner.com" programmeType="Movie"/>
<MovieInfo>
<Movie id="1159775" name="Vertigo">
<ProductionYear>1958</ProductionYear>
<Studio>Warner</Studio>
<Titles>
<Title locale="ES-AR" type="EPG">Vertigo(ESP)</Title>
<Title locale="EN-US" type="EPG">Vertigo (US)</Title>
</Titles>
<Synopses>
<Synopsis locale="EN-US">
<Short/>
<Medium/>
<Long>Description text english</Long>
</Synopsis>
<Synopsis locale="ES-AR">
<Short/>
<Medium/>
<Long>Description text spanish</Long>
</Synopsis>
</Synopses>
<Genres>
<Genre sequence="1">Drama</Genre>
<Genre sequence="2">Thriller</Genre>
<Genre sequence="3">Mystery</Genre>
</Genres>
</Movie>
</MovieInfo>
</VODMetadata>'''
root = ET.fromstring(xml)
print(root.find('.//Title[@locale="EN-US"]').text)
print(root.find('.//Synopsis[@locale="EN-US"]').find('Long').text)输出
Vertigo (US)
Description text englishhttps://stackoverflow.com/questions/64522285
复制相似问题