系统管理中,经常涉及的文件编码就是UTF8和GB1803,下面是实现iconv简化功能(UTF8,GB18030互转)的python代码:
def to_unicode(str_a):
if type(str_a) is unicode:
return str_a
try:
u=str_a.decode('utf-8')
return u
except:
try:
u=str_a.decode('gb18030')
return u
except:
pass
return str_a
def iconv(file,to,from_t='',sep=False):
u'''
sep :是否转换换行符
'''
if os.path.exists(file):
try:
import re
f=open(file,'rb')
lines=f.readlines()
f.close()
new_lines=[]
for v in lines:
if from_t!='':
s=v.decode(from_t).encode(to)
else:
s=to_unicode(v).encode(to)
if sep:
if re.match('utf.*',to,re.I):
s=re.sub('\r\n$','\n',s,re.I)
else:#gbk:使用windows换行符
s=re.sub('\r\n$','\n',s,re.I)
s=re.sub('\n$','\r\n',s,re.I)
new_lines.append(s)
import shutil
shutil.move(file, file+'.bak')
f=open(file,'wb')
f.writelines(new_lines)
f.close()
return NORMAL
except:
return ERROR
return ERROR