Problème d'encodage utf-8/iso-8859-15
Bonjour,
J'ai un problème d'encodage (Python 2.6.6): j'ai un fichier texte où les données sont enregistrées en iso-8859-15 (sous windows) contenant une unique ligne :
Mon script est sous Linux :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import codecs
if __name__ == '__main__':
fichierTextWin = codecs.open('fichierTextWin.txt', 'r', 'iso-8859-15')
lignes = fichierTextWin.readlines()
print(lignes)
for ligne in lignes:
print ligne
if '\xe9\n' == ligne:
print("1 - ça marche")
else:
print("1 - ça ne marche pas")
if 'é' == ligne:
print("2 - ça marche")
else:
print("2 - ça ne marche pas")
if u'é' == ligne:
print("3 - ça marche")
else:
print("3 - ça ne marche pas") |
et ça donne
Code:
1 2 3 4 5 6 7 8 9
| [u'\xe9\r\n']
test_UTF-8_Linux.py:16: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
if '\xe9\n' == ligne:
1 - ça ne marche pas
test_UTF-8_Linux.py:20: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
if 'é' == ligne:
2 - ça ne marche pas
3 - ça ne marche pas |
J'ai essayé dans l'autre sens: en enregistrant le fichier texte en utf-8 cette fois ci et là:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
import codecs
if __name__ == '__main__':
fichierTextWin = codecs.open('fichierTextWin.txt', 'r', 'utf-8')
lignes = fichierTextWin.readlines()
print(lignes)
for ligne in lignes:
print ligne
if u'\xe9\n' == ligne:
print("1 - ça marche")
else:
print("1 - ça ne marche pas")
if u'é' == ligne:
print("2 - ça marche")
else:
print("2 - ça ne marche pas") |
ça donne
Code:
1 2
| 1 - ça marche
2 - ça ne marche pas |
Comment faire pour faire une conversion correcte sans avoir à écrire d'hexa ?
Merci d'avance !!