Convert UTF16LE file to UTF8 in Python?

杀马特。学长 韩版系。学妹 提交于 2020-01-01 20:58:23

问题


I have big file with utf16le (BOM) encoding.
Is it possible to convert it to usual UTF8 by python?

Something like

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)

Not working. Can't understand "TypeError: must be str, not bytes" error.
python 3


回答1:


You should not be encoding it. Let the stdlib do its job.

file_new.write(text)


来源:https://stackoverflow.com/questions/4633444/convert-utf16le-file-to-utf8-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!