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

青葱年少 python 957

原文标题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 而失败。请用另一种方法帮助我。

原文链接:https://stackoverflow.com//questions/71508111/convert-utf-16-to-utf-8-using-python

回复

我来回复
  • hiro protagonist的头像
    hiro protagonist 评论

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

    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', 'rb', encoding='utf-16') as source_file, \
            open(r'D:\_apps\aaa\output\destfile', 'w+b', encoding='utf-8') as dest_file:
        for line in source_file:
            dest_file.write(line)
    
    2年前 0条评论