簡體   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