如何处理u前缀utf8字符串(需要解码,但失败时为“u”)?

2024-05-18 14:51:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python构建一个与python3兼容的web基文件管理器。在

每个文件头都是:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import

然后我发现由于Unicode的原因有问题。在

我的系统是中文的,我有一个名为E:\filemanager\data\c - 副本的文件夹。在

我使用ajax GET发送(E:\filemanager\data\c - 副本)到flask应用程序(filemanager)。当我遇到表示路径不存在的Windows Error[3]时,我也很困惑。然后我使用pycharm调试代码,看到字符串已经变成了u'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac',当os.listdir(path)。在

这根绳子太可怕了。让我展示一下问题:

^{pr2}$

p2中有一个u前缀!这使我无法列出目录(因为名称错误)。在

我知道只有去掉u前缀才能使解码成功。但是如何移除它呢?在


更新

我要的是皈依

u'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'

u'E:\\filemanager\\data\\c - \u526f\u672c'或{}

由于u前缀,\xe5\x89\xaf\xe6\x9c\xac无法解码,这是关键问题!在


更新2:

我的ajax代码:

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
})

Tags: 文件pathurldatagetparamsmethodoptions
2条回答

unicode字符串可以编码到字节str
str可以被解码为unicode字符串。在

当您有一个u''字符串文本时,当您import unicode_literals时,所有的字符串文本都将是unicode字符串。你只能对那些代码进行编码,而不是decode。当您尝试decode一个已经解码的unicode字符串时,您得到的错误源于隐式转换。在

>>> p1.encode('utf-8')
'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'

\x35…表示字符串的原始字节(str)。在

^{pr2}$

这是一个unicode文本,字面意思是“\xe5…”。
当有原始字节表示时,需要确保Python将其视为str,而不是unicode

>>> p2 = b'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'
>>> p2.decode('utf-8')
u'E:\\filemanager\\data\\c - \u526f\u672c'

前缀b将文本标记为str,可以将其解码为unicode。在

u''''unicode_literals和{}是unicodeencode到{}
b''和{}是strdecode到{}

What I want is convert

u'E:\filemanager\data\c - \xe5\x89\xaf\xe6\x9c\xac' to

either u'E:\filemanager\data\c - \u526f\u672c' or 'E:\filemanager\data\c - \xe5\x89\xaf\xe6\x9c\xac'

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

无法解码Unicode字符串。关键问题是UTF-8编码的字节字符串在一开始就被错误地解码了。在

下面是如何逆转它,但你真正应该解决的是为什么一开始就错了。在

latin1是将前256个Unicode码位直接转换为字节的编解码器:

>>> s = u'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'
>>> s.encode('latin1')
'E:\\filemanager\\data\\c - \xe5\x89\xaf\xe6\x9c\xac'

所以“摆脱美国”。现在有一个可以用UTF-8解码的字节字符串:

^{pr2}$

相关问题 更多 >

    热门问题