Вопрос

Using python, how can I convert a string like

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

to

id=215903184&index=0&st=52&sid=95000&ip=14.145.245.85&pw=

update
the specific problem is when I request a url, and return:

 {"code":0,"urls":["http://vr.tudou.com/v2proxy/v?id\u003d217267950\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d113.68.97.224\u0026pw\u003d"]}

so, I want to parse this real urls can access to

Это было полезно?

Решение

In Python 3, the strings are identical. In Python 2, if the string is not hard-coded in your script (in which case you can use a Unicode literal, as Roberto suggests), you can decode it:

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='

Другие советы

Just use:

>>> 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='

Try this one:

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

Try this ,

>>> 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='
>>> 

Update

>>> 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='
>>> 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top