2014-09-08 3 views
0

J'utilise Python 2.7.8 et j'ai un script qui n'analyse:Unicode ANSI ou UTF8 en python

def parse(): 
    with open('myfile.txt') as f: 
. 
. 
. 
Print l 

myfile.txt a un codage UNICODE. Comment puis-je ajouter un code à ce script afin qu'il lise le fichier myfile.txt en tant qu'ANSI par exemple?

+1

Pourquoi ne pas utiliser 'unicode' en interne? Utilisez 'io.open()' et spécifiez le codage. –

+0

Matériel de lecture: http://nedbatchelder.com/text/unipain.html, https://docs.python.org/2/howto/unicode.html et http://www.joelonsoftware.com/articles/Unicode .html –

+1

[ANSI] (https://fr.wikipedia.org/wiki/American_National_Standards_Institute) - n'est pas un encodage. Voulez-vous dire [ASCII] (https://en.wikipedia.org/wiki/ASCII)? Dans ce cas, python s'ouvre en codage ASCII par défaut. – stalk

Répondre

2

en utilisant io résoudre mon problème:

import io  
def parse() 
    with io.open(path,'r',encoding='utf_16') as f: 
..... 

résolu mon problème.

+0

VACANTE SAINTE VACHE !!! Merci beaucoup! J'ai cherché des jours et vous avez enfin répondu à ma question! –

Questions connexes