我想用python解码字符串

我想用python解码字符串,python,unicode,decode,Python,Unicode,Decode,使用python,如何转换字符串,如 'id\u003d215903184\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d14.145.245.85\u0026pw\u003d' 到 更新 具体问题是当我请求url并返回: {"code":0,"urls":["http://vr.tudou.com/v2proxy/v?id\u003d217267950\u0026index\u003d0\u0026st

使用python,如何转换字符串,如

'id\u003d215903184\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d14.145.245.85\u0026pw\u003d'

更新
具体问题是当我请求url并返回:

 {"code":0,"urls":["http://vr.tudou.com/v2proxy/v?id\u003d217267950\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d113.68.97.224\u0026pw\u003d"]}
所以,我想解析这个真正的URL可以访问的

只需使用:

>>> s = u'id\u003d215903184\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d14.145.245.85\u0026pw\u003d'
>>> s
u'id=215903184&index=0&st=52&sid=95000&ip=14.145.245.85&pw='
试试这个:

u'id\u003d215903184\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d14.145.245.85\u0026pw\u003d'

在Python 3中,字符串是相同的。在Python 2中,如果脚本中没有对字符串进行硬编码(在这种情况下,正如Roberto所建议的,可以使用Unicode文本),则可以对其进行解码:

In [1]: s = 'id\u003d215903184\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d14.145.245.85\u0026pw\u003d'

In [2]: s.decode('unicode-escape')
Out[2]: u'id=215903184&index=0&st=52&sid=95000&ip=14.145.245.85&pw='
试试这个

>>> import unicodedata
>>> unicodedata.normalize('NFKD', u'id\u003d215903184\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d14.145.245.85\u0026pw\u003d').encode('ascii','ignore')
'id=215903184&index=0&st=52&sid=95000&ip=14.145.245.85&pw='
>>> 
更新
我会避免在问题的上下文中命名内置str类型这样的变量,至少应该尝试一下,当遇到特定问题时,可以问一个特定的问题。这不是一个代码编写服务,虽然有时看起来是这样。至少在谷歌搜索会得到你写的答案。不是我的反对票,但为什么要立即将其编码为ASCII码?只需将其保留为unicode。@lvc感谢更新的答案,请查看。
>>> import unicodedata
>>> unicodedata.normalize('NFKD', u'id\u003d215903184\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d14.145.245.85\u0026pw\u003d').encode('ascii','ignore')
'id=215903184&index=0&st=52&sid=95000&ip=14.145.245.85&pw='
>>> 
>>> unicode_str=u'id\u003d215903184\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d14.145.245.85\u0026pw\u003d'
>>> normal_str = unicode_str.encode('utf-8')
>>> normal_str
'id=215903184&index=0&st=52&sid=95000&ip=14.145.245.85&pw='
>>> unicode_str = normal_str.decode('utf-8')
>>> unicode_str
u'id=215903184&index=0&st=52&sid=95000&ip=14.145.245.85&pw='
>>>