Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3 Convert all characters to HTML Entities

I'm using Python3 and I wonder if there is a module or a default function for converting all characters of a text to html entities (even the letters and digits) because I don't want to make a translation map for this.


Solved: As @justhalf told me, I found the solution by making this function:

def htmlEntities( string ):
    return ''.join(['&#{0};'.format(ord(char)) for char in string])
like image 287
Mircea Avatar asked Oct 21 '25 02:10

Mircea


1 Answers

If you want to really escape all characters, there is no default function for that, but you can just replace each character with the ordinals manually:

''.join('&%d;'.format(ord(x)) for x in string)
like image 76
justhalf Avatar answered Oct 23 '25 15:10

justhalf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!