csv文件编码转换:解决utf-8编码的文件在excel打开时中文乱码问题。
Python完整代码如下:
# -*- coding:utf-8 -*-
# date = 20201231
#将utf-8编码的csv文件转换为gb2312编码的csv文件
import chardet
import codecs
from sys import argv
script,file_name = argv
file_in = file_name
file_out = 'gb2312-'+file_name
encode_in = 'utf-8'
encode_out = 'gb2312'
with codecs.open(filename=file_in,mode='r',encoding=encode_in) as fin:
data = fin.read()
with open(file_out,mode='w',encoding=encode_out,newline='') as fout:
fout.write(data)
fout.close()
with open(file_out,'rb') as f:
data = f.read()
print(chardet.detect(data)['encoding'])
命名为:utf-8-2-gb2312.py,使用时在cmd命令行输入如下:
>python utf-8-2-gb2312.py filename.csv
输出结果:
GB2312
在filename.csv同目录下生成文件:gb2312-filename.csv。
备注:
with open(file_out,mode='w',encoding=encode_out,newline='') as fout:
这里要设置“ newline='' ”,否则在新生成的csv文件中会产生空行。