问题
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])
回答1:
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)
来源:https://stackoverflow.com/questions/18609778/python3-convert-all-characters-to-html-entities