csv文件編碼轉(zhuǎn)換:解決utf-8編碼的文件在excel打開時中文亂碼問題垒迂。
Python完整代碼如下:
# -*- coding:utf-8 -*-
# date = 20201231
#將utf-8編碼的csv文件轉(zhuǎn)換為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
輸出結(jié)果:
GB2312
在filename.csv同目錄下生成文件:gb2312-filename.csv妒蛇。
備注:
with open(file_out,mode='w',encoding=encode_out,newline='') as fout:
這里要設(shè)置“ newline='' ”机断,否則在新生成的csv文件中會產(chǎn)生空行。