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>

<blockquote>
  <p>What I want is convert</p>
  
  <p>u'E:\filemanager\data\c - \xe5\x89\xaf\xe6\x9c\xac' to</p>
  
  <p>either u'E:\filemanager\data\c - \u526f\u672c' or 'E:\filemanager\data\c - \xe5\x89\xaf\xe6\x9c\xac'</p>
  
  <p>\xe5\x89\xaf\xe6\x9c\xac can not be decode due to the u prefix, , that is the key problem!</p>
</blockquote>

<p>You can't decode Unicode strings.  The key problem is that a UTF-8-encoded byte string was decoded incorrectly in the first place.</p>

<p>Here's how to reverse it, but what you should really solve is why it was wrong to begin with.</p>

<p><code>latin1</code> is a codec that converts the first 256 Unicode codepoints directly to bytes:</p>

<pre><code>&gt;&gt;&gt; s = u'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'
&gt;&gt;&gt; s.encode('latin1')
'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'
</code></pre>

<p>So that "gets rid of the u".  You now have a byte string that can be decoded with UTF-8:</p>

<pre><code>&gt;&gt;&gt; s.encode('latin1').decode('utf8')
u'E:\\filemanager\\data\\c - \u526f\u672c'
&gt;&gt;&gt; print s.encode('latin1').decode('utf8')
E:\filemanager\data\c - å¯æ¬
</code></pre>

python python-2.7 unicode