-1
我試圖替換這個xml中的值,我想要替換的是這個ip 10.10.10.75所有匹配項。使用python查找xml文件中的替換
<profile name="internal">
<settings>
<param name="rtp-ip" value="10.10.10.75"/>
<!-- ip address to bind to, DO NOT USE HOSTNAMES ONLY IP ADDRESSES -->
<param name="sip-ip" value="10.10.10.75"/>
<param name="presence-hosts" value="10.10.10.75,10.10.10.75"/>
</settings>
</profile>
這是我的示例代碼
#!/usr/bin/python
from shutil import copyfile
import xml.etree.ElementTree as ET
#try:
# copyfile('/usr/src/sample.xml','/usr/src/sample3.xml')
# print "Profile Copied Sucessfully"
#except IOError as e:
# print "I/O error({0}): {1}".format(e.errno, e.strerror)
with open('/usr/src/sample3.xml') as f:
tree = ET.parse(f)
root = tree.getroot()
for elem in root.getiterator():
try:
elem.text = elem.text.replace('10.10.10.75', '10.10.10.100')
# elem.text = elem.text.replace('FEATURE NUMBER', '123456')
except AttributeError:
pass
tree.write('/usr/src/sample3.xml')
這沒有得到我想要的東西,它的去除註釋行也,我不想做的事。