<p>当您在终端中输入“äß”,虽然您看到的是“äß”,但终端会看到字节。如果您的终端编码是<code>utf-8</code>,那么它会看到字节</p>
<pre><code>In [2]: 'äèß'
Out[2]: '\xc3\xa4\xc3\xa8\xc3\x9f'
</code></pre>
<p>所以当你打字的时候</p>
^{pr2}$
<p>在命令行,终端(假设使用utf-8编码)看到</p>
^{3}$
<p>这不是您打算发送给Python的unicode。在</p>
<pre><code>In [28]: print(u'\xc3\xa4\xc3\xa8\xc3\x9f')
äèÃ
</code></pre>
<p>有很多方法可以解决这个问题,也许是为了方便:</p>
<ol>
<li><p>让终端将<code>äèß</code>更改为<code>\xc3\xa4\xc3\xa8\xc3\x9f</code>,然后
将其解码为<code>utf-8</code>:</p>
<pre><code>% python -c "from unidecode import *; print unidecode('äèß'.decode('utf_8'))"
aess
</code></pre></li>
<li><p>如Nehal J.Wani的解决方案所示,声明一个编码:</p>
<pre><code>% python -c "#coding: utf8
> from unidecode import *; print unidecode(u'äèß')"
aess
</code></pre>
<p>但是,这需要在两行上编写命令。</p></li>
<li><p>因为<code>u'äèß</code>相当于<code>u'\xe4\xe8\xdf'</code>,所以可以避免
通过传递<code>u'\xe4\xe8\xdf'</code>来解决问题:</p>
<pre><code>% python -c "from unidecode import *; print unidecode(u'\xe4\xe8\xdf')"
aess
</code></pre>
<p>这样做的问题(很明显)是你必须考虑
输出十六进制码位值。</p></li>
<li><p>或者,可以指定unicode名称:</p>
<pre><code>% python -c "from unidecode import *; print unidecode(u'\N{LATIN SMALL LETTER A WITH DIAERESIS}\N{LATIN SMALL LETTER E WITH GRAVE}\N{LATIN SMALL LETTER SHARP S}')"
aess
</code></pre></li>
</ol>