Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case Insensitive findall in Python ElementTree

Tags:

python

xml

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 totally case insensitive in ElementTree?

   # Does not work
   variables = message.findall("VaRiAbLE")
like image 376
01100110 Avatar asked Feb 25 '12 03:02

01100110


1 Answers

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)
like image 154
Snakes and Coffee Avatar answered Oct 12 '22 17:10

Snakes and Coffee