- 测试环境:python3.7
cat xx.py
# -*- encoding:utf8 -*-
import os, sys
def iconv(filename,scr_type,obj_type):
cc = open('{}'.format(filename),'r',encoding='{}'.format(scr_type))
ccc = cc.read()
cccc = ccc.encode('{}'.format(scr_type)).decode('gbk')
cc.close()
cc = open('{}'.format(filename),'w',encoding='{}'.format(obj_type))
cc.write(cccc)
cc.close()
if len(sys.argv) != 4:
print("Usage:python" + sys.argv[0] + "/data/xx.txt iso8859-1 utf8")
if __name__ == '__main__':
filename = sys.argv[1]
scr_type = sys.argv[2]
obj_type = sys.argv[3]
iconv(filename,scr_type,obj_type)
- 测试
# cat xx.txt
֢ˇISO8859¹þ¹þ
# python xx.py xx.txt iso8859-1 utf8
# cat xx.txt
这是ISO8859哈哈