使用 python 将 utf-16 转换为 utf-8

Convert utf-16 to utf-8 using python

我正在尝试使用 python 将一个巨大的 csv 文件从 utf-16 格式转换为 utf-8 格式,下面是代码:

with open(r'D:\_apps\aaa\output\srcfile, 'rb') as source_file:
            with open(r'D:\_apps\aaa\output\destfile, 'w+b') as dest_file:
                contents = source_file.read()
                dest_file.write(contents.decode('utf-16').encode('utf-8'))

但是此代码使用大量内存并因 Memoryerror 而失败。请帮我换个方法。

一个选项是逐行转换文件:

with open(r'D:\_apps\aaa\output\srcfile', 'rb') as source_file, \
        open(r'D:\_apps\aaa\output\destfile', 'w+b') as dest_file:
    for line in source_file:
        dest_file.write(line.decode('utf-16').encode('utf-8'))

或者您可以使用所需的编码打开文件:

with open(r'D:\_apps\aaa\output\srcfile', 'r', encoding='utf-16') as source_file, \
        open(r'D:\_apps\aaa\output\destfile', 'w+', encoding='utf-8') as dest_file:
    for line in source_file:
        dest_file.write(line)