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

问题描述

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

I need to convert a bunch of files to utf-8 in Python, and I have trouble with the "converting the file" part.

我想做相当于:

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

谢谢!

推荐答案

您可以使用,如下所示:

You can use the codecs module, like this:

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 参数控制文件块大小。

EDIT: added BLOCKSIZE parameter to control file chunk size.

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

08-20 11:39