python解析xml以获取大于给定值的字符串

python解析xml以获取大于给定值的字符串,python,xml,string,parsing,Python,Xml,String,Parsing,如何使用python解析xml文件,我试图在退出时获得大于50的“分数”。在我的xml文件中,它确实存在,它应该打印出65,93 Test.xml <analysis xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <description/> <alert url="/alert/224.xml" message="Hello"/> <warning url=

如何使用python解析xml文件,我试图在退出时获得大于50的“分数”。在我的xml文件中,它确实存在,它应该打印出65,93

Test.xml

    <analysis xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <description/>
    <alert url="/alert/224.xml" message="Hello"/>
    <warning url="/warning/2.xml">
    <score>65</score>
    </warning>
    <warning url="/warning/23.xml">
    <score>33</score>
    </warning>
    <warning url="/warning/233.xml">
    <score>93</score>
    </warning>
    <warning url="/warning/233.xml">
    <score>93</score>
    </warning>
    </analysis>

65
33
93
93

您可以使用
BeautifulSoup
解析
xml
文件。然后,对于每个分数,我们可以将该分数添加到
集合
,这意味着没有重复(即,我们不会输出
93
两次)

其中:

65 93
使用美化组

from bs4 import BeautifulSoup
score_set=set()
soup = BeautifulSoup(open('Test.xml'),"html.parser")
for score in soup.findAll('score'):
    if (int(score.next_element)>50):
        score_set.add(int(score.next_element))
print(score_set) # {65, 93}

非常感谢您引入“BeautifulSoup”导入。非常感谢Joe,我认为下面的行需要更新,(open('Test.xml'),“html.parser”)。@Mihir取决于您需要什么样的解析器,但是是的,所有python安装都有标准的“html.parser”。非常感谢James。不用担心,您不需要多次循环,只需一次迭代即可完成所有工作,但我尝试将其拆分以显示各个阶段
from bs4 import BeautifulSoup
score_set=set()
soup = BeautifulSoup(open('Test.xml'),"html.parser")
for score in soup.findAll('score'):
    if (int(score.next_element)>50):
        score_set.add(int(score.next_element))
print(score_set) # {65, 93}
import xml.etree.ElementTree as ET

tree = ET.parse("Test.xml")

warnings = tree.findall("warning")

values = map(lambda x: x.getchildren()[0].text, warnings)

print ','.join(set(filter(lambda f: int(f)> 50, values)))