d = dict((chr(code), '&%s;' % name) for code, name in codepoint2name.
    items() if code != 38)
if '&' in text:
    text = text.replace('&', '&')
for key, value in d.items():
    if key in text:
        text = text.replace(key, value)
return text
How to convert special characters into html entities?