python - SOLVED: Python3 Convert all characters to HTML Entities -


i'm using python3 , wonder if there module or default function converting characters of text html entities (even letters , digits) because don't want make translation map this.

later edit: @justhalf told me, found solution making function:

def htmlentities( string ):     return ''.join(['&#{0};'.format(ord(char)) char in string]) 

html.parser job

 >>> import html.parser >>> h = html.parser.htmlparser() >>> print(h.unescape('£682m')) £682m >>> print(h.escape('£682m')) £682m 

or if want escape characters, can replace each character ordinals:

''.join('&%d;'.format(ord(x)) x in string) 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -