热线电话:13121318867

登录
2020-02-23 阅读量: 1194
Python如何批量修改csv文件编码格式为utf8

可以使用os库以及chardet库中的方法,进行遍历循环文件名称,然后修改编码格式

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)
27.5220
4
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子