擅长:python、mysql、java
<p>Python 3将<code>unicode</code>类型重命名为<code>str</code>,旧的<code>str</code>类型已被<code>bytes</code>替换。</p>
<pre><code>if isinstance(unicode_or_str, str):
text = unicode_or_str
decoded = False
else:
text = unicode_or_str.decode(encoding)
decoded = True
</code></pre>
<p>您可能需要阅读<a href="http://docs.python.org/dev/howto/pyporting.html">Python 3 porting HOWTO</a>以了解更多此类详细信息。还有Lennart Regebro的<a href="http://python3porting.com/">Porting to Python 3: An in-depth guide</a>,免费在线。</p>
<p>最后但并非最不重要的是,您可以尝试使用<a href="http://docs.python.org/3/library/2to3.html">^{<cd5>} tool</a>来查看如何为您翻译代码。</p>