发布时间:2019-08-05 15:33:48编辑:auto阅读(5112)
工作中需要将繁体中文转换成简体中文
上网找了些资料,发现这个包最方便
不需要什么安装方法,只需要把这两个文件下载下来,保存到与代码同一目录下即可
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/zh_wiki.py
from langconv import *
def Traditional2Simplified(sentence):
'''
将sentence中的繁体字转为简体字
:param sentence: 待转换的句子
:return: 将句子中繁体字转换为简体字之后的句子
'''
sentence = Converter('zh-hans').convert(sentence)
return sentence
if __name__=="__main__":
traditional_sentence = '憂郁的臺灣烏龜'
simplified_sentence = Traditional2Simplified(traditional_sentence)
print(simplified_sentence)
'''
输出结果:
忧郁的台湾乌龟
'''
from langconv import *
def Simplified2Traditional(sentence):
'''
将sentence中的简体字转为繁体字
:param sentence: 待转换的句子
:return: 将句子中简体字转换为繁体字之后的句子
'''
sentence = Converter('zh-hant').convert(sentence)
return sentence
if __name__=="__main__":
simplified_sentence = '忧郁的台湾乌龟'
traditional_sentence = Simplified2Traditional(simplified_sentence)
print(traditional_sentence)
'''
输出结果:
憂郁的臺灣烏龜
'''
from langconv import *
def Traditional2Simplified(sentence):
'''
将sentence中的繁体字转为简体字
:param sentence: 待转换的句子
:return: 将句子中繁体字转换为简体字之后的句子
'''
sentence = Converter('zh-hans').convert(sentence)
return sentence
def Simplified2Traditional(sentence):
'''
将sentence中的简体字转为繁体字
:param sentence: 待转换的句子
:return: 将句子中简体字转换为繁体字之后的句子
'''
sentence = Converter('zh-hant').convert(sentence)
return sentence
if __name__=="__main__":
traditional_sentence = '憂郁的臺灣烏龜'
simplified_sentence = Traditional2Simplified(traditional_sentence)
print(simplified_sentence)
skydark:https://github.com/skydark/nstools/tree/master/zhtools
上一篇: python批量创建gitlab的用户
下一篇: python3 类排序 类比较
47461
45754
36761
34278
28933
25565
24413
19584
19068
17607
5437°
6019°
5533°
5611°
6535°
5348°
5348°
5855°
5827°
7140°