python批量修改txt文件,csv文件 编码格式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python批量修改txt文件,csv文件 编码格式相关的知识,希望对你有一定的参考价值。

技术分享图片



from os import listdir
from chardet import detect

fns = (fn for fn in listdir() if fn.endswith(‘.csv‘))

for fn in fns:
    with open(fn, ‘rb+‘) as fp:
        content = fp.read()
        encoding = detect(content)[‘encoding‘]
        content = content.decode(encoding).encode(‘utf8‘)
        fp.seek(0)
        fp.write(content)

批量改为utf-8编码

以上是关于python批量修改txt文件,csv文件 编码格式的主要内容,如果未能解决你的问题,请参考以下文章