天天看點

python實作的簡版iconv

系統管理中,經常涉及的檔案編碼就是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