思路:引入简繁体处理库,有兴趣的同学可以研究一下内部实现,都是python写的
1、下载zh_wiki.py及langconv
zh_wiki.py:https://github.com/skydark/nstools/blob/master/zhtools/zh_wiki.py
langconv.py:https://github.com/skydark/nstools/blob/master/zhtools/langconv.py
下载langconv.py和zh_wiki.py,放在python代码所在目录即可
2、代码实例
# @param python实现中文字符繁体和简体中文转换
# @author 编程之家 jb51.cc|www.512Pic.com
# -*- coding:utf-8 -*-
from langconv import *
# 转换繁体到简体
line = Converter('zh-hans').convert(line.decode('utf-8'))
line = line.encode('utf-8')
# 转换简体到繁体
line = Converter('zh-hant').convert(line.decode('utf-8'))
line = line.encode('utf-8')
# End www.jb51.cc
原文链接:https://www.f2er.com/python/526942.html