Python ElementTree XML解析与多个答案

时间:2015-02-07 18:32:30

标签: python xml-parsing elementtree

我正在解析My Movies 5生成的movie.xml文件以获取电影的类型。有些电影有多种类型,如:

-<Genres>
<Genre>Adventure</Genre>
<Genre>Comedy</Genre>
<Genre>Action</Genre>
...
...
</Genres>

如何将其作为单个变量阅读 genres = genere1,genre2,genre3 ...

这就是我正在做的事情,它只给了我第一个类型:

import xml.etree.ElementTree as ET
tree = ET.parse('movie.xml')
root = tree.getroot()
Genre = tree.findtext("Genres/Genre")

缩短的movie.xml如下:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--This file is created by My Movies (http://www.mymovies.dk)-->
<Title IsBoxSetParent="False" IsBoxSetChild="False">
  <ID>649</ID>
  <MediaType>Movie</MediaType>
  <LocalTitle>Six Days, Seven Nights</LocalTitle>
  <ProductionYear>1998</ProductionYear>
  <ReleaseDate>12/8/1998</ReleaseDate>
  <RunningTime>101</RunningTime>
  <TagLine />
  <Genres>
    <Genre>Adventure</Genre>
    <Genre>Comedy</Genre>
    <Genre>Action</Genre>
    <Genre>Romance</Genre>
  </Genres>
  <AudioTracks>
    <AudioTrack Language="English" Type="Dolby Digital" Channels="5.1" />
    <AudioTrack Language="French" Type="Dolby Digital" Channels="5.1" />
  </AudioTracks>
  <CheckSum>f98f43ba468b519bb7e78c15b7ab9cfa</CheckSum>
</Title>

2 个答案:

答案 0 :(得分:1)

谢谢,这比我提出的其他方式更优雅。

genre=""
for element in root.iter("Genre"):
        genre = genre + ", " + ("%s" % (element.text))
print genre

产生相同的: Adventure, Comedy, Action, Romance

答案 1 :(得分:0)

您可以尝试findall()来电,使用map提取文字,join创建包含所有结果的字符串,例如:

import xml.etree.ElementTree as ET
tree = ET.parse('movie.xml')
root = tree.getroot()
Genre = ', '.join(map(lambda e: e.text, tree.findall("Genres/Genre")))
print(Genre)

产量:

Adventure, Comedy, Action, Romance