Case Insensitive findall in Python ElementTree

前端 未结 2 567
无人共我
无人共我 2021-01-12 16:53

I have to parse XML that has tag names that may be in any case (mixed, upper, lower, etc) and I don\'t know what the case will be beforehand. How can I make findall be total

相关标签:
2条回答
  • 2021-01-12 17:31

    You simply get the string from the tree, lowercase it, and remake the tree. Then it should be parseable

    import xml.etree.ElementTree as ET
    def to_parseable(tree):
        t = ET.tostring(tree)
        t = t.lower()
        return ET.fromstring(t)
    
    0 讨论(0)
  • 2021-01-12 17:50

    Regex to the rescue. Note this is probably horrific in terms of performance but is great at extracted XML attributes from elements.

    def getInsensitiveAttrbiute(element, key) :
    
       keyRegex = re.compile(key, re.IGNORECASE)
       for key in element.attrib.keys() :
           if keyRegex.match(key) :
               return element.attrib[key]
       raise KeyError
    
    
    element = ET.fromstring('<FOO src="BAR" />')
    print getInsensitiveAttrbiute(element, "sRc")
    
    0 讨论(0)
提交回复
热议问题