How to cope u prefix utf-8 string( need to decode but fail by `u` )?
<p>I am using python build a web base file manager with python 3 compatibility.</p>

<p>Every file header is:</p>

<pre><code># -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
</code></pre>

<p>Then I found something wrong due to Unicode.</p>

<p>My system is Chinese,  I have a folder named <code>E:\filemanager\data\c - å¯æ¬</code>.</p>

<p>I use <code>ajax GET</code> send (<code>E:\filemanager\data\c - å¯æ¬</code>) to the flask app(filemanager). I also confusing when I meet the <code>Windows Error[3]</code> which indicate path not exists. Then I use pycharm to debug my code, and see the string has already became <code>u'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'</code> when <code>os.listdir(path)</code>.</p>

<p>This string is horrible. Let me show the problem:</p>

<pre><code>&gt;&gt;&gt; p1 = ur'E:\filemanager\data\c - å¯æ¬'
&gt;&gt;&gt; p1
u'E:\\filemanager\\data\\c - \u526f\u672c'
&gt;&gt;&gt; p1.encode('utf-8')
'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'
# here you can see this result just like p2 without `u` prefix

&gt;&gt;&gt; p2 = u'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'
&gt;&gt;&gt; p2.decode('utf-8')
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "C:\Users\kasim\Envs\fmgr\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 24-29: ordinal not in range(128)
</code></pre>

<p>There is a <code>u</code> prefix in <code>p2</code>! Which make me can not list the directory(because the name is wrong).</p>

<p>I know only remove that <code>u</code> prefix can make decode successful. But How to remove it ?</p>

<hr>

<p><strong>UPDATE</strong>:</p>

<p>What I want is convert</p>

<p><code>u'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'</code> to </p>

<p>either <code>u'E:\\filemanager\\data\\c - \u526f\u672c'</code> or <code>'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'</code></p>

<p><code>\xe5\x89\xaf\xe6\x9c\xac</code> can not be decode due to the <code>u</code> prefix, , that is the key problem!</p>

<hr>

<p><strong>UPDATE2:</strong></p>

<p>my ajax code:</p>

<pre><code>function ajax2(options) {

  var querystring = options.params ? urllib.format({ query: options.params }) : ''

  if (options.loading)
    swalShowLoading();

  reqwest({
      url: options.url + querystring,
      contentType: 'application/json',
      method: options.method || 'GET',
      data: JSON.stringify(options.data)
  })

}

// getFileList
// because this is a `GET` method, so no data be JSON.stringify here
// sorry for wrong explanation before.
ajax2({
  url: ApiUrl.list,
  params: {
    path: encodeURI(path)
  },
  success:success,
  error:error
})
</code></pre>

<p><code>unicode</code> strings can be <em>encoded</em> to byte <code>str</code>.<br>
<code>str</code> can be <em>decoded</em> to <code>unicode</code> strings.</p>

<p>When you have a <code>u''</code> string literal, <em>or</em> when you <code>import unicode_literals</code>, all your string literals will be <code>unicode</code> strings. You can only <em>encode</em> those, not <code>decode</code>. The error you get stems from an implicit conversion when you try to <code>decode</code> an already decoded <code>unicode</code> string.</p>

<pre><code>&gt;&gt;&gt; p1.encode('utf-8')
'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'
</code></pre>

<p>The <code>\x35â¦</code> represent the raw bytes (<code>str</code>) of your string.</p>

<pre><code>u'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'
</code></pre>

<p>This is a <code>unicode</code> literal which literally expresses the text "\xe5â¦".<br>
When you have a raw byte representation, you need to make sure Python treats it as a <code>str</code>, not as <code>unicode</code>:</p>

<pre><code>&gt;&gt;&gt; p2 = b'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'
&gt;&gt;&gt; p2.decode('utf-8')
u'E:\\filemanager\\data\\c - \u526f\u672c'
</code></pre>

<p>The <code>b</code> prefix marks the literal as <code>str</code>, which can be decoded into a <code>unicode</code>.</p>

<p><code>u''</code>, <code>''</code> with <code>unicode_literals</code> and <code>\u526f</code> are <code>unicode</code> â <code>encode</code> to <code>str</code><br>
<code>b''</code> and <code>\xe5</code> are <code>str</code> â <code>decode</code> to <code>unicode</code></p>

python python-2.7 unicode