开发者

Python - Display string containing entity references as normal text

开发者 https://www.devze.com 2022-12-18 20:25 出处:网络
I have a Python string \"''Grassmere''\" as retrieved from a website.

I have a Python string "''Grassmere''" as retrieved from a website.

I would like 开发者_如何学编程to have the ' displayed as the correct ascii symbol (') but for some reason python insists on just printing the ascii code.


Batteries are included for this one

>>> import xmllib
>>> X=xmllib.XMLParser()
>>> X.translate_references("''Grassmere''")
"''Grassmere''"


Or without additional modules:

re.sub("&#(\d+);", lambda m: chr(int(m.group(1))), "''Grassmere''")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号