0

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

liuzhijun
  • 4,329
  • 3
  • 23
  • 27
  • 3
    You should at least try something, and ask a specific question when you encounter a specific problem. This isn't a code writing service, although sometimes it seems that way. – juanchopanza Apr 25 '14 at 08:38
  • atleast a search in google will fetch u write answer..http://stackoverflow.com/questions/19527279/python-unicode-to-ascii-conversion – sundar nataraj Apr 25 '14 at 08:45

4 Answers4

2

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='
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
1

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='
Roberto Reale
  • 4,247
  • 1
  • 17
  • 21
0

Try this one:

u'id\u003d215903184\u0026index\u003d0\u0026st\u003d52\u0026sid\u003d95000\u0026ip\u003d14.145.245.85\u0026pw\u003d'
dorvak
  • 9,219
  • 4
  • 34
  • 43
0

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='
>>> 
Nishant Nawarkhede
  • 8,234
  • 12
  • 59
  • 81