뭔가를 신속하고 더러운 방법이 라이브러리에 내장되지 않았거나 잘 모르겠지만,). 10 진수, 16 진수 및 htmlentitydefs에있는 명명 된 엔터티를 처리합니다.
import re
from htmlentitydefs import name2codepoint
EntityPattern = re.compile('&(?:#(\d+)|(?:#x([\da-fA-F]+))|([a-zA-Z]+));')
def decodeEntities(s, encoding='utf-8'):
def unescape(match):
code = match.group(1)
if code:
return unichr(int(code, 10))
else:
code = match.group(2)
if code:
return unichr(int(code, 16))
else:
code = match.group(3)
if code in name2codepoint:
return unichr(name2codepoint[code])
return match.group(0)
if isinstance(s, str):
s = s.decode(encoding)
return EntityPattern.sub(unescape, s)
마크 : 감사합니다! – Shane
@YOU 샘플을 표시 할 수 있습니까? 여기에 텍스트를 입력 할 수 있습니까? 죄송합니다, Python에 익숙하지 않습니다. –