擅长:python、mysql、java
<p>你在把苹果和桔子混合。GBK编码的字符串不是Unicode字符串,因此不应以<code>u'...'</code>字符串结尾。</p>
<p>在Python 2中,这是正确的方法。</p>
<pre><code>g = '\xc7\xeb\xca\xe4\xc8\xeb\xd5\xfd\xc8\xb7\xd1\xe9\xd6\xa4\xc2\xeb,' \
'\xd0\xbb\xd0\xbb!'.decode('gbk')
s = u"<script language=javascript>alert(" + g +
u");location='index.asp';</script></script>"
</code></pre>
<p>请注意,传递给<code>.decode('gbk')</code>的<code>g</code>的初始值设定项如何不表示为Unicode字符串,而是表示为纯字节字符串。</p>
<p>另见<a href="http://nedbatchelder.com/text/unipain.html" rel="nofollow">http://nedbatchelder.com/text/unipain.html</a></p>