2011-01-08 142 views
0

我有utf16le(BOM)編碼的大文件。
是否有可能通過python將其轉換爲通常的UTF8?python UTF16LE文件以UTF8編碼

喜歡的東西

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)

不工作。無法理解「TypeError:必須是str,而不是字節」錯誤。
python 3

+0

http://docs.python.org/py3k/library/codecs.html#standard-encodings – 2011-01-08 10:49:53

回答

2

您不應該對其進行編碼。讓stdlib完成它的工作。

file_new.write(text)