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

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编码

猜你喜欢

转载自blog.51cto.com/13000661/2129573