Python实现繁體转为简体的方法示例

时间:2022-09-26 07:30:03

本文实例讲述了Python实现繁體转为简体的方法。分享给大家供大家参考,具体如下:

这里需要用到两个文件,可以点击此处本站下载源文件:zh_wiki.py  和 langconv.py

或者从github下载: https://github.com/csdz/nstools/tree/master/zhtools

转换函数:

?
1
2
3
4
5
6
7
8
9
10
11
from langconv import *
def tradition2simple(line):
 # 将繁体转换成简体
 line = Converter('zh-hans').convert(line.decode('utf-8'))
 line = line.encode('utf-8')
 return line
def tradition2simple2(line):
 # 将繁体转换成简体
 line = Converter('zh-hans').convert(line)
 line = line.encode('utf-8')
 return line

测试代码

?
1
print tradition2simple2(u"無所謂")

#輸出
无所谓

注:这里需要安装psycopg模块,可点击https://www.lfd.uci.edu/~gohlke/pythonlibs/选择对应的版本进行安装。

希望本文所述对大家Python程序设计有所帮助。

原文链接:https://blog.csdn.net/Tangzongyu123/article/details/77115949