我有以下形式编码的字符串:La + Cit%C3%A9 + De + la + West,我存储在python中的SQLite VARCHAR字段中.
这些显然是转换为urlencoded字符串的UTF-8编码二进制字符串.问题是如何将其转换回unicode字符串.s ='La + Cit%C3%A9 + De + la + West'
我使用了urllib.unquote_plus(s)python函数,但它没有将%C3%A9转换为unicode char.我看到这个'LaCitédela West'而不是预期的'LaCitéDela West'.
我在Ubuntu上运行我的代码,而不是Windows,编码是UTF-8.
正如我们所讨论的,看起来问题是你是从一个unicode对象开始的,而不是一个字符串.你想要一个字符串:
>>> import urllib
>>> s1 = u'La+Cit%C3%A9+De+la+West'
>>> type(s1)
<type 'unicode'>
>>> print urllib.unquote_plus(s1)
La Cité De la West
>>> s2 = str(s1)
>>> type(s2)
<type 'str'>
>>> print urllib.unquote_plus(s2)
La Cité De la West
>>> import sys
>>> sys.stdout.encoding
'UTF-8'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5346 次 |
| 最近记录: |