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]