擅长:python、mysql、java
<p>您可以使用<strong>xml</strong>模块中的<strong>escape</strong>函数</p>
<pre class="lang-py prettyprint-override"><code>from xml.sax.saxutils import escape
my_string = "Some string with an &"
# If the string contains &, <, or > they will be converted.
print(escape(my_string))
# Above will return: Some string with an &amp;
</code></pre>
<p>引用:<a href="https://stackoverflow.com/questions/1546717/escaping-strings-for-use-in-xml">Escaping strings for use in XML</a></p>