2011-01-08 7 views
0

J'ai un gros fichier avec encodage utf16le (BOM).
Est-il possible de le convertir en UTF8 habituel en python?Python Fichier UTF16LE au codage UTF8

Quelque chose comme

file_old = open('old.txt', mode='r', encoding='utf-16-le') 
file_new = open('new.txt', mode='w', encoding='utf-8') 

text = file_old.read() 

file_new.write(text.encode('utf-8')) 

http://docs.python.org/release/2.3/lib/node126.html (- utf_16_le UTF-16LE)

ne fonctionne pas. Impossible de comprendre l'erreur "TypeError: doit être str, pas bytes".
python 3

+0

http://docs.python.org/py3k/library/codecs.html#standard-encodings –

Répondre

2

Vous ne devriez pas l'encoder. Laissez le stdlib faire son travail.

file_new.write(text)