简体   繁体   中英

How to decode ascii combined with string in python

I am trying to decode and ascii which is combined with string

example

g&#108bo&#115w&#111&#114t&#104

But i am not getting exact output

'g&#108bo&#115w&#111&#114t&#104'.decode("ascii")

output

u'g&#108bo&#115w&#111&#114t&#104'

if u remove this characters &# and try only with integers i get this

>>> chr(108)
'l'
>>> chr(115)
's'
>>> chr(111)
'o'
>>> chr(114)
'r'
>>> chr(104)
'h'

expected output

glbosworth

How can i decode this one "g&#108bo&#115w&#111&#114t&#104" to expected output

you are trying to decode html escaped string . you can use the html.unescape(s) function to do so (on python3):

import html
print(html.unescape('g&#108bo&#115w&#111&#114t&#104'))

outputs:

'glbosworth'

take a look at this so answer for more info

  • on python3.6.x you can use html.unescape :

     import html print(html.unescape('g&#108bo&#115w&#111&#114t&#104')) 
  • on python 2.x you can use HTMLParser :

     from HTMLParser import HTMLParser h = HTMLParser() print(h.unescape('g&#108bo&#115w&#111&#114t&#104')) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM