使用,
lxml您可以遍历所有元素并检查其是否包含以下文字
strip():
from lxml import etreetree = etree.parse('xmlfile')root = tree.getroot()for elem in root.iter('*'): if elem.text is not None: elem.text = elem.text.strip()print(etree.tostring(root))
它产生:
<Person><name>My Name</name><Address>My Address</Address></Person>
更新 也剥离
tail文本:
from lxml import etreetree = etree.parse('xmlfile')root = tree.getroot()for elem in root.iter('*'): if elem.text is not None: elem.text = elem.text.strip() if elem.tail is not None: elem.tail = elem.tail.strip()print(etree.tostring(root, encoding="utf-8", xml_declaration=True))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)