<p>我在使用unicode字符串时遇到的主要问题是,将utf-8编码的字符串与unicode编码的字符串混合在一起。</p>
<p>例如,考虑以下脚本。</p>
<p>2.py年</p>
<pre><code># encoding: utf-8
name = 'helló wörld from two'
</code></pre>
<p>一个.py</p>
<pre><code># encoding: utf-8
from __future__ import unicode_literals
import two
name = 'helló wörld from one'
print name + two.name
</code></pre>
<p>运行<code>python one.py</code>的输出是:</p>
<pre><code>Traceback (most recent call last):
File "one.py", line 5, in <module>
print name + two.name
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 4: ordinal not in range(128)
</code></pre>
<p>在这个例子中,<code>two.name</code>是一个utf-8编码的字符串(不是unicode),因为它没有导入<code>unicode_literals</code>,而<code>one.name</code>是一个unicode字符串。当两者混合使用时,python尝试解码编码的字符串(假设它是ascii)并将其转换为unicode,但失败了。如果你这样做的话就行了。</p>
<p>如果对字符串进行编码并稍后尝试混合它们,同样的事情也可能发生。
例如,它可以工作:</p>
<pre><code># encoding: utf-8
html = '<html><body>helló wörld</body></html>'
if isinstance(html, unicode):
html = html.encode('utf-8')
print 'DEBUG: %s' % html
</code></pre>
<p>输出:</p>
<pre><code>DEBUG: <html><body>helló wörld</body></html>
</code></pre>
<p>但是在添加<code>import unicode_literals</code>之后,它不会:</p>
<pre><code># encoding: utf-8
from __future__ import unicode_literals
html = '<html><body>helló wörld</body></html>'
if isinstance(html, unicode):
html = html.encode('utf-8')
print 'DEBUG: %s' % html
</code></pre>
<p>输出:</p>
<pre><code>Traceback (most recent call last):
File "test.py", line 6, in <module>
print 'DEBUG: %s' % html
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 16: ordinal not in range(128)
</code></pre>
<p>它失败,因为<code>'DEBUG: %s'</code>是一个unicode字符串,因此python尝试解码<code>html</code>。修复打印的两种方法是执行<code>print str('DEBUG: %s') % html</code>或<code>print 'DEBUG: %s' % html.decode('utf-8')</code>。</p>
<p>我希望这有助于您理解使用unicode字符串时可能出现的问题。</p>