回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<blockquote>
<p><strong>Possible Duplicate:</strong><br/>
<a href="https://stackoverflow.com/questions/3531430/how-to-refer-to-sign-in-python-string">How to refer to “\” sign in python string</a> </p>
</blockquote>
<p>我有相当大的字符串数据,其中我必须删除除A-Z、A-Z和0-9以外的所有字符
我几乎可以删除所有字符,但“\”是个问题。你知道吗</p>
<p>其他字符都会被删除,但“\”会产生问题</p>
<pre><code>def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
reps = {' ':'-','.':'-','"':'-',',':'-','/':'-',
'<':'-',';':'-',':':'-','*':'-','+':'-',
'=':'-','_':'-','?':'-','%':'-','!':'-',
'$':'-','(':'-',')':'-','\#':'-','[':'-',
']':'-','\&':'-','@':'-','\W':'-','\t':'-'}
x.name = x.name.lower()
x1 = replace_all(x.name,reps)
</code></pre>