Python 繁体中文与简体中文相互转换

1 安装

不需要什么安装方法,只需要把这两个文件下载下来,保存到与代码同一目录下即可

https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py

https://raw.githubusercontent.com/skydark/nstools/master/zhtools/zh_wiki.py

 

2.使用

将一个目录下的所有文件夹及文件的名字都转换成中文简体并且去除中间的空格

import os
from langconv import *

def simple2tradition(line):
    # 将简体转换成繁体
    line = Converter('zh-hant').convert(line)
    return line

def tradition2simple(line):
    # 将繁体转换成简体
    line = Converter('zh-hans').convert(line)
    return line


def renameDirTradition2simple(rootFolder):
    for rt, dirs, files in os.walk(rootFolder):
        toString = rt + '\n'
        toString = toString + "    ==============dirs===============" + '\n'
        for folder in dirs:
            dirPath = os.path.join(rt, folder)
            newDirPath = os.path.join(rt, tradition2simple(folder.replace(" ", "")))
            os.rename(dirPath, newDirPath)
            toString = toString + "    rename " + dirPath + " [to] " + newDirPath + '\n'
            renameDirTradition2simple(newDirPath)
        toString = toString + "    ==============files===============" + '\n'
        for f in files:
            filePath = os.path.join(rt, f)
            newFilePath = os.path.join(rt, tradition2simple(f.replace(" ", "")))
            os.rename(filePath, newFilePath)
            toString = toString + "    rename " + filePath + " [to] " + newFilePath + '\n'
        print(toString)
        break


if __name__ == '__main__':
    root = r"E:\中文简体繁体"   #请事先把含有中文繁体名字的文件夹及文件放到root目录下
    renameDirTradition2simple(root)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值