restore_utf8、utf8togbk

restore_utf8.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Run "python restore_utf8.py" to rename *.h.utf8 to *.h.
#

import os

def restore_utf8(dir):
resultfn = ''
for fn in os.listdir(dir):
sfile = os.path.join(dir, fn)
if os.path.isdir(sfile):
resultfn += restore_utf8(sfile)
continue
if fn.endswith('.utf8'):
orgfile = sfile[:-5]
try:
if os.path.exists(orgfile): os.remove(orgfile)
os.rename(sfile, orgfile)
resultfn += fn[:-5] + ' '
except:
print('except for %s' %(fn,))
return resultfn

if __name__=="__main__":
resultfn = restore_utf8(os.path.abspath('.'))
resultfn += restore_utf8(os.path.abspath('../core'))
resultfn += restore_utf8(os.path.abspath('../android'))
if resultfn != '': print('restore files: ' + resultfn)




#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Run "python utf8togbk.py" to convert source code files to the GBK format on Windows.
#

import os, codecs, sys

reload(sys)
sys.setdefaultencoding('gbk')

def utf8togbk(dir):
resultfn = ''
for fn in os.listdir(dir):
sfile = os.path.join(dir, fn)
if os.path.isdir(sfile):
resultfn += utf8togbk(sfile)
continue
if fn.endswith('.h') or fn.endswith('.cpp'):
if os.path.exists(sfile + '.utf8'):
continue
try:
text = open(sfile,'r',-1,'utf-8').read()
oldtext = text
except UnicodeDecodeError:
continue
except TypeError:
text = open(sfile).read()
oldtext = text
try:
if text[:3] == codecs.BOM_UTF8:
u = text[3:].decode('utf-8')
text = u.encode('gbk')
except UnicodeEncodeError:
continue
except UnicodeDecodeError:
continue
try:
text = text.replace('\r\n','\n')
text = text.replace('\n','\r\n')
if cmp(text, oldtext) != 0:
os.rename(sfile, sfile + '.utf8')
open(sfile, 'wb').write(text)
resultfn += fn + ' '
st = os.stat(sfile + '.utf8')
os.utime(sfile, (st.st_atime, st.st_mtime))
except:
print('except for %s' %(fn,))
return resultfn

if __name__=="__main__":
resultfn = utf8togbk(os.path.abspath('.'))
resultfn += utf8togbk(os.path.abspath('../core'))
resultfn += utf8togbk(os.path.abspath('../android'))
if resultfn != '': print('utf8->gbk: ' + resultfn)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值