我需要在Python中将一堆文件转换为utf-8,并且在“转换文件”部分遇到麻烦。

我想做相当于:

iconv -t utf-8 $file > converted/$file # this is shell code

谢谢!

最佳答案

您可以使用codecs module,如下所示:

import codecs
BLOCKSIZE = 1048576 # or some other, desired size in bytes
with codecs.open(sourceFileName, "r", "your-source-encoding") as sourceFile:
    with codecs.open(targetFileName, "w", "utf-8") as targetFile:
        while True:
            contents = sourceFile.read(BLOCKSIZE)
            if not contents:
                break
            targetFile.write(contents)

编辑:添加了BLOCKSIZE参数以控制文件块大小。

10-08 02:21