I am trying to replace special characters with HTML entities, But the results are random with the same input and I don't understand why.
Here is the code :
def secure(text):
hsconvert = {"\'": "\\'", "\"": "\\\"", "¢": "¢", "©": "©", "÷": "÷", ">": ">", "<": "<", "µ": "µ", "·": "·", "¶": "¶", "±": "±", "€": "€", "£": "£", "®": "®", "§": "§", "™": "™", "¥": "¥", "á": "á", "Á": "Á", "à": "à", "À": "À", "â": "â", "Â": "Â", "å": "å", "Å": "Å", "ã": "ã", "Ã": "Ã", "ä": "ä", "Ä": "Ä", "æ": "æ", "Æ": "Æ", "ç": "ç", "Ç": "Ç", "é": "é", "É": "É", "è": "è", "È": "È", "ê": "ê", "Ê": "Ê", "ë": "ë", "Ë": "Ë", "í": "í", "Í": "Í", "ì": "ì", "Ì": "Ì", "î": "î", "Î": "Î", "ï": "ï", "Ï": "Ï", "ñ": "ñ", "Ñ": "Ñ", "ó": "ó", "Ó": "Ó", "ò": "ò", "Ò": "Ò", "ô": "ô", "Ô": "Ô", "ø": "ø", "Ø": "Ø", "õ": "õ", "Õ": "Õ", "ö": "ö", "Ö": "Ö", "ß": "ß", "ú": "ú", "Ú": "Ú", "ù": "ù", "Ù": "Ù", "û": "û", "Û": "Û", "ü": "ü", "Ü": "Ü", "ÿ": "ÿ", "\\":"\\\\"};
for i, j in hsconvert.items():
text = text.replace(i, j)
return text
print(secure("La Vie d'Adèle, chapitres 1 & 2"))
Here are the console outputs:
>>> ================================ RESTART ================================
>>>
La Vie d\'Adèle, chapitres 1 & 2
['TV Movie', 'Video Game', 'TV Episode', 'TV Series', 'TV Series ', 'Short', 'TV Mini-Series']
>>> ================================ RESTART ================================
>>>
La Vie d\\'Adèle, chapitres 1 & 2
['TV Movie', 'Video Game', 'TV Episode', 'TV Series', 'TV Series ', 'Short', 'TV Mini-Series']
The problem is with the '
character which is sometimes returned as \'
and sometimes as \\'
.
I think it is coming from the last item in the dictionary, "\\":"\\\\"
but I don't understand why it is not interpreted the same on each run.