<p><code>str.decode</code>不只是在字符串文本前面加上<code>u</code>。它将输入字符串的字节转换为有意义的字符(即Unicode)。</p>
<p>然后调用<code>encode</code>将这些字符转换为字节,因为您需要“打印”,将它们输出到终端或任何其他操作系统实体(如GUI窗口)。</p>
<p>所以,关于你的具体任务,我相信你想要的是:</p>
<pre><code>s = '\x80'
print s.decode('cp1251').encode(platform_encoding)
</code></pre>
<p>其中<code>'cp1251'</code>是IDE的编码,而<code>platform_encoding</code>是当前系统的编码变量。</p>
<hr/>
<p>在回复您的评论时:</p>
<blockquote>
<p>But the str.decode should have used the source code encoding (from
line 2 in the file) to decode. So there should not be a difference to
the u</p>
</blockquote>
<p>这是错误的假设。来自<a href="http://www.python.org/dev/peps/pep-0263/" rel="nofollow noreferrer">Defining Python Source Code Encodings</a></p>
<blockquote>
<p>The encoding information is then used by the Python parser to
interpret the file using the given encoding.</p>
</blockquote>
<p>所以<code>set fileencoding=cp1252</code>只是告诉解释器在解析第<code>str = '\x80'</code>行时如何将[通过编辑器输入的]字符转换为字节。在<code>str.decode</code>调用期间不使用此信息。</p>
<p>你也在问,u'\x80'是什么?<code>\x80</code>简单地解释为<code>\u0080</code>,这显然不是您想要的。看看这个问题-<a href="https://stackoverflow.com/questions/9845842/bytes-in-a-unicode-python-string">Bytes in a unicode Python string</a>。</p>