Python3简繁体转换
Python3简繁体转换
github---->https://github.com/skydark/nstools/tree/master/zhtools
具体方法很简单,下载该项目中的 zh_wiki.py 和 langconv.py 两个文件,放到python代码目录下就可以了。
例如:
创建util包将下载zh_wiki.py和langconv.py放到该目录下面,然后新建simplified_traditional.py文件
simplified_traditional.py
说明:该demo指传入参数为列表,转换后在返回列表
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'tian'
__data__ = '2019/3/26 17:51'
from util.langconv import Converter #
def cat_to_chs(sentence): #传入参数为列表
"""
将繁体转换成简体
:param line:
:return:
"""
sentence =",".join(sentence)
sentence = Converter('zh-hans').convert(sentence)
sentence.encode('utf-8')
return sentence.split(",")
def chs_to_cht(sentence):#传入参数为列表
"""
将简体转换成繁体
:param sentence:
:return:
"""
sentence =",".join(sentence)
sentence = Converter('zh-hant').convert(sentence)
sentence.encode('utf-8')
return sentence.split(",")
if __name__ == '__main__':
li_1 = ['鸡', '鸡', '虎', '牛', '猪', '虎', '兔']
li_2 = ['雞', '雞', '虎', '牛', '豬', '虎', '兔']
rest_fon = chs_to_cht(li_1) #简体转换成繁体
print("简体转换成繁体:{0}".format(rest_fon))
rest_chinese = cat_to_chs(li_2)
print("简体转换成简体:{0}".format(rest_chinese))