2017-01-22 42 views
0

我想通過用正確的值替換標籤屬性來清理XML文件。ElementTree替換標籤屬性並更新文件

當我在下面運行我的代碼時,標記屬性會更新,但只有在元素樹標記對象中,XML文件不會被更新/保存。

有沒有辦法在ET.iterparse中更新/保存對XML對象所做的更改?在我的循環中更改了所有標記屬性值之後,是否有更新文件的方法?

當我在更改前後打印出tag.attrib ['v']時,它會正確更新爲正確的值。但是我保存的XML文件沒有反映出這些變化。

我發現的所有解決方案都沒有使用ET.iterparse方法。我正在處理一個相當大的文件,並希望保留我對ET.iterparse的使用。

使用:

  • 的Python 2.7
  • xml.etree.cElementTree

感謝。

def writeClean(self, cleaned_streets): 
    ''' 
    Get cleaned streets mapping dictionary and use that dictionary to find 
    and replace all bad street name tag attributes within XML file. 

    Iterate through XML file to find all bad instances of tag attribute 
    street names, and replace with correct mapping value from cleaned_streets 
    mapping dictionary. 

    ''' 
    with open(self.getSampleFile(), 'r+') as f:    
     for event, elem in ET.iterparse(f, events=('start',)): 
      if elem.tag == 'node' or elem.tag == 'way': 
       for tag in elem.iter('tag'): 
        if self.isStreetName(tag): 
         street = tag.attrib['v'] 
         if street in cleaned_streets.keys(): 
          tag.attrib['v'] = cleaned_streets[street] 

回答

0

使用ElemetTree你可以這樣做:

tree = ET.parse(file_path) 
    # Do some modification to your file 
    tree.write(file_path) 

比如我有一段代碼是這樣的:

def __update_cfg_file(self): 
    try: 
     tree = ET.parse(self._config_file_path) 
     root = tree.getroot() 
     add_file_element = "./input/additional-files" 

     root.find(add_file_element).attrib["value"] = "additional-files" 

     tree.write(self._config_file_path) 

    except IOError: 
     sys.exit("Something went wrong while opening %s" 
       % (self._config_file_path))