考虑以下:
>>> import re
>>> a = "first:second"
>>> re.findall("[^:]*", a)
['first', '', 'second', '']
>>> re.sub("[^:]*", r"(\g<0>)", a)
'(first):(second)'
Run Code Online (Sandbox Code Playgroud)
re.sub()最初的行为更有意义,但我也能理解re.findall()行为.毕竟,你可以匹配之间的空字符串 first和:仅包含非冒号字符(其中恰好为零),但为什么不re.sub()表现的一样吗?
不应该是最后一个命令的结果(first)():(second)()吗?
我的xml文件是:
<A>
<B>some text</B>
<B>other text</B>
<B>more text</B>
</A>
Run Code Online (Sandbox Code Playgroud)
我想要做的是<B></B>从xml中删除第二个.我不知道它有什么文字.但我有索引<B></B>,比如index = 1,这意味着我想删除第二个元素(或节点).
我有这样的代码:
F = open('example.xml')
self.tree = parse(F)
self.root = self.tree.getroot()
F.close()
Run Code Online (Sandbox Code Playgroud)
所以在这种情况下我要删除的是self.root[1].
如何使用ElementTree实现?
编辑:让我的问题更清晰,更具体.