在Python中将UTF16LE文件转换为UTF8吗? [英] Convert UTF16LE file to UTF8 in Python?

查看:662
本文介绍了在Python中将UTF16LE文件转换为UTF8吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用utf16le(BOM)编码的大文件。

是否可以通过python将其转换为普通的UTF8?

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

类似

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)

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

不起作用。无法理解 TypeError:必须为str,而不是字节错误。

python 3

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

推荐答案

您不应该对其进行编码。

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

file_new.write(text)

这篇关于在Python中将UTF16LE文件转换为UTF8吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆