2017-04-06 50 views
0

我想在xml中搜索文件的子字符串與任何模式都不匹配的元素,然後從根中刪除元素。如何知道整個字符串是否與模式部分匹配?

  1. 我想知道有其他的方法來搜索是否完整路徑的子串用的模式的一個或不匹配。

  2. 我想知道如何從根中刪除匹配項。

您能否讓我得到一些建議。

這裏是我的示例腳本

if __name__ == '__main__': 

    root = ElementTree() 
    root.parse('errors.xml') 
    patterns = ['/cm-audio/src/main/', '/reprogram-manager/'] 
    for error in errors: 
     file = error.find('file').text 
     bMatch = False 
     for p in patterns : 
      if p in file : 
       bMattch = True 
       break 
     if bMatch == False : 
      root.remove(error) #it doesn't work. 

回答

0
if __name__ == '__main__': 
    import re 
    pylines='<date><Signature xmlns="http://www.w3.org/2000/09/xmldsig#">dd<SignedDataencoding="base64"><Data>cc</Data><date>' 
    way1='<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">(.*?)<SignedDataencoding="base64"><Data>(.*?)</Data>' 
    partens = re.compile(way1,re.S) 
    items1 = re.findall(partens,pylines) 
    print items1 

嗨,你可以使用重新模型蟒蛇例如像這些, 如果要刪除,你可以節省其他的XML跳過的數學條件

0

對於它的價值,你有一個命名錯誤。

bMattch = True 

應該

bMatch = True 

不這樣做,bMatch永遠不能True

相關問題