繁体   English   中英

如何在Python中解码结合字符串的ASCII

[英]How to decode ascii combined with string in python

我正在尝试解码和结合字符串的ASCII

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

但是我没有得到确切的输出

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

输出

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

如果您删除此字符&#并仅尝试使用整数我会得到这个

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

预期产量

glbosworth

我如何将这一个“ g&#108bo&#115w&#111&#114t&#104”解码为预期的输出

您正在尝试解码html转义的字符串 您可以使用html.unescape(s)函数这样做(在python3上):

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

输出:

'glbosworth'

看看这个,所以回答更多信息

  • 在python3.6.x上,您可以使用html.unescape

     import html print(html.unescape('g&#108bo&#115w&#111&#114t&#104')) 
  • 在python 2.x上,您可以使用HTMLParser

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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