Python从文档中剥离XML标签
问题内容:
我正在尝试使用Python(一种我刚接触的语言)从文档中剥离XML标记。这是我第一次使用regex,这确实是一个最好的主意。
mfile = file("somefile.xml","w")
for line in mfile:
re.sub('<./>',"",line) #trying to match elements between < and />
那惨败了。我想知道如何用正则表达式来完成。
其次,我在Google上搜索并找到:http : //code.activestate.com/recipes/440481-strips-
xmlhtml-tags-from-string/
这似乎有效。但是我想知道有没有更简单的方法来摆脱所有xml标签?也许使用ElementTree?
问题答案:
请注意,通常用正则表达式来做是不正常的。见耶利米回答。
尝试这个:
import re
text = re.sub('<[^<]+>', "", open("/path/to/file").read())
with open("/path/to/file", "w") as f:
f.write(text)