Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何为具有特定属性值的xml元素选择数据,并根据键区分多个元素?_Python_Xml_Parsing_Beautifulsoup_Lxml_Xpath - Fatal编程技术网

Python 如何为具有特定属性值的xml元素选择数据,并根据键区分多个元素?

Python 如何为具有特定属性值的xml元素选择数据,并根据键区分多个元素?,python,xml,parsing,beautifulsoup,lxml,xpath,Python,Xml,Parsing,Beautifulsoup,Lxml,Xpath,鉴于: 使用xml.etree.ElementTree 有没有更好的解决办法?一种线性解决方案?单向使用: 它产生: hello 以下是您如何处理和: 您的xml数据格式不正确。你应该把它修好。 xmlKeyValue = fieldvalue.get('id') if xmlKeyValue == "234": sol = fieldvalue.get('value') print sol print(soup.find(

鉴于:

使用xml.etree.ElementTree

有没有更好的解决办法?一种线性解决方案?

单向使用:

它产生:

hello

以下是您如何处理和:

您的xml数据格式不正确。你应该把它修好。
   xmlKeyValue = fieldvalue.get('id')

   if xmlKeyValue == "234":

       sol  = fieldvalue.get('value')

       print sol
print(soup.find('stream').find('max').find('bar', attrs={'id':'234'})['value'])
hello
from lxml import etree

xml = """
<stream>
    <max id="500">
        <bar id="233" value="hell"/>
        <bar id="234" value="hello"/>
    </max>
</stream>"""

xml = etree.fromstring(xml)
print xml.xpath('//max/bar[@id=234]/@value')  # ['hello']