我需要在Python中将一堆文件转换为utf-8,并且在“转换文件”部分遇到麻烦。
我想做相当于:
iconv -t utf-8 $file > converted/$file # this is shell code
谢谢!
您可以使用codecs模块,如下所示:
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
参数以控制文件块大小。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句