Python ElementTree中不区分大小写的findall [英] Case Insensitive findall in Python ElementTree

查看:25
本文介绍了Python ElementTree中不区分大小写的findall的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须解析具有在任何情况下都可能存在的标记名称(混合、上、下等)的 XML,但我事先不知道会是什么情况.如何让 findall 在 ElementTree 中完全不区分大小写?

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")

推荐答案

您只需从树中获取字符串,将其小写,然后重新制作树.那么它应该是可解析的

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)

这篇关于Python ElementTree中不区分大小写的findall的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆