Python 批量修改 汉语转拼音

如果看过我上一篇博客的人可能会记得我之前进行批量处理图片大小的经历,就在那之后,经过一系列的步骤之后,我们终于得到了梦寐以求的Bundle包,只可惜是中文的,500多个包啊。全部是中文的。但是Unity对于中文路径的资源是 可以 写 不可以 读。所以需要改成对应的拼音,(如果有高手大哥可以转换成英文的话,可以给我留言哦,谢谢),下面就是我的python脚本了,修改文件夹下以“.unity3d”为后缀的所有文件的中文名-》拼音名字;

备注:

1.导入pypinyin库,可以使用里面的lazy_pinyin类。参考:https://pypi.python.org/pypi/pypinyin/0.5.0

2.导入了send2trash库,同样是删除不需要的文件到垃圾箱内(就是unity的配置文件.meta文件啦)

# -*- coding: gb18030 -*-   
import sys, os, glob
from pypinyin import pinyin, lazy_pinyin
import pypinyin
from send2trash import send2trash

reload(sys)
sys.setdefaultencoding('utf-8')

rename_postfix=".unity3d"
delete_postfix=".meta"

def Py_Log(_string):
    print "----"+_string.decode('utf-8')+"----"

def rename_file(file_path):
    mPath, ext = os.path.splitext(file_path)

    if(ext==rename_postfix):
        ch_name=get_ch_name(mPath)
        if(has_chinese(ch_name)):
            py_name=get_py_name(ch_name);
            print "名字转换:   "+unicode(ch_name,'gbk')+"   ==>   "+py_name
            names = os.path.split(file_path)
            new_path = names[0]+"\\"+py_name+rename_postfix
            if (os.path.exists(new_path)):
                print "\""+py_name+"\"文件已经存在请修改后重试"
                raw_input('press enter key to continue')
            else:
                os.rename(file_path,new_path)
    elif(ext==delete_postfix):
        send2trash(file_path)
        print "Delete "+delete_postfix+" file :"+file_path

def has_chinese(text):
    hz_yes = False
    for ch in unicode(text,"gbk"):
        if ch >= u'\u4e00' and ch<=u'\u9fa5':
            hz_yes=True
        else:
            continue
    return hz_yes

def get_ch_name(mPath):
    tmp = mPath.split('\\')
    ch_name=tmp[len(tmp)-1]
    ch_name=ch_name.strip('0123456789')
    return ch_name

def get_py_name(ch_name):
    queue=""
    py_name=lazy_pinyin(unicode(ch_name,'gbk'))
    
    for pn in py_name:
        queue+=pn.capitalize()
    return queue

def BFS_Dir(dirPath, dirCallback = None, fileCallback = None):
    queue = []
    ret = []
    queue.append(dirPath);
    while len(queue) > 0:
        tmp = queue.pop(0)
        if(os.path.isdir(tmp)):
            ret.append(tmp)
            for item in os.listdir(tmp):
                queue.append(os.path.join(tmp, item))
            if dirCallback:
                dirCallback(tmp)
        elif(os.path.isfile(tmp)):
            ret.append(tmp)
            if fileCallback:
                fileCallback(tmp)
    return ret

def printDir(dirPath):
    print "dir: " + dirPath

def printFile(dirPath):
    print "file: " + dirPath
    rename_file(dirPath)

if __name__ == '__main__':
    while True:
        path = raw_input("Path:")
        try:
            b = BFS_Dir(path , printDir, printFile)
            Py_Log ("\r\n          **********\r\n"+"*********文件处理完毕*********"+"\r\n          **********\r\n")
        except:
            print "Unexpected error:", sys.exc_info()
        raw_input('press enter key to exit')
    

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TxNet.Ltd.

你的赞同是对我的鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值