擅长:python、mysql、java
<p>这样怎么样:</p>
<pre><code>placeholder = "Value_to_search_for"
new_value = "New_Value"
for line in open("yourfile.xml"):
output_line = line
if "<id>" in line:
beginning_index = line.index("<id>")
end_index = line.index("</id>")+5 # 5 = The number of characters in '</id>'
output_line = line
output_line = output_line[beginning_index:end_index].replace(placeholder, new_value)
print (output_line)
</code></pre>
<p>它在标记“id”中查找值的开头和结尾的索引,并用新值替换其中的内容。你知道吗</p>