【问题标题】:Convert UTF16LE file to UTF8 in Python?在 Python 中将 UTF16LE 文件转换为 UTF8?
【发布时间】:2011-01-08 10:30:50
【问题描述】:

我有 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”错误。
蟒蛇3

【问题讨论】:

标签: python encoding


【解决方案1】:

您不应该对其进行编码。让标准库完成它的工作。

file_new.write(text)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 2023-03-21
    • 2013-01-04
    相关资源
    最近更新 更多