如何在Python中将UTF16LE文件转换为UTF8?
我有一个很大的文件,它是用utf16le(带有BOM)编码的。
请问可以用Python把它转换成常见的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: must be str, not bytes”这个错误。
我用的是Python 3。
1 个回答
2
你不需要自己去编码,让标准库来处理这些事情就可以了。
file_new.write(text)