Python實(shí)現(xiàn)繁體中文與簡體中文相互轉(zhuǎn)換的方法示例
本文實(shí)例講述了Python實(shí)現(xiàn)繁體中文與簡體中文相互轉(zhuǎn)換的方法。分享給大家供大家參考,具體如下:
工作中需要將繁體中文轉(zhuǎn)換成簡體中文
上網(wǎng)找了些資料,發(fā)現(xiàn)這個(gè)包最方便:https://github.com/skydark/nstools/tree/master/zhtools
安裝方法
不需要什么安裝方法,只需要把這兩個(gè)文件下載下來,保存到與代碼同一目錄下即可
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/langconv.py
https://raw.githubusercontent.com/skydark/nstools/master/zhtools/zh_wiki.py
或者點(diǎn)擊此處本站下載源文件:zh_wiki.py 和 langconv.py
繁體轉(zhuǎn)簡體:
from langconv import * def Traditional2Simplified(sentence): ''' 將sentence中的繁體字轉(zhuǎn)為簡體字 :param sentence: 待轉(zhuǎn)換的句子 :return: 將句子中繁體字轉(zhuǎn)換為簡體字之后的句子 ''' sentence = Converter('zh-hans').convert(sentence) return sentence if __name__=="__main__": traditional_sentence = '憂郁的臺(tái)灣烏龜' simplified_sentence = Traditional2Simplified(traditional_sentence) print(simplified_sentence) ''' 輸出結(jié)果: 憂郁的臺(tái)灣烏龜 '''
簡體轉(zhuǎn)繁體:
from langconv import * def Simplified2Traditional(sentence): ''' 將sentence中的簡體字轉(zhuǎn)為繁體字 :param sentence: 待轉(zhuǎn)換的句子 :return: 將句子中簡體字轉(zhuǎn)換為繁體字之后的句子 ''' sentence = Converter('zh-hant').convert(sentence) return sentence if __name__=="__main__": simplified_sentence = '憂郁的臺(tái)灣烏龜' traditional_sentence = Simplified2Traditional(simplified_sentence) print(traditional_sentence) ''' 輸出結(jié)果: 憂郁的臺(tái)灣烏龜 '''
完整代碼:
from langconv import * def Traditional2Simplified(sentence): ''' 將sentence中的繁體字轉(zhuǎn)為簡體字 :param sentence: 待轉(zhuǎn)換的句子 :return: 將句子中繁體字轉(zhuǎn)換為簡體字之后的句子 ''' sentence = Converter('zh-hans').convert(sentence) return sentence def Simplified2Traditional(sentence): ''' 將sentence中的簡體字轉(zhuǎn)為繁體字 :param sentence: 待轉(zhuǎn)換的句子 :return: 將句子中簡體字轉(zhuǎn)換為繁體字之后的句子 ''' sentence = Converter('zh-hant').convert(sentence) return sentence if __name__=="__main__": traditional_sentence = '憂郁的臺(tái)灣烏龜' simplified_sentence = Traditional2Simplified(traditional_sentence) print(simplified_sentence)
參考資料:
skydark:https://github.com/skydark/nstools/tree/master/zhtools
PS:這里再為大家推薦幾款功能相似的在線工具供大家參考:
中文繁體字簡體字轉(zhuǎn)換(繁簡轉(zhuǎn)換)工具:
http://tools.jb51.net/transcoding/convertzh
在線自動(dòng)排版與轉(zhuǎn)換工具:
http://tools.jb51.net/aideddesign/txt_beaut
在線文字/文本排版/轉(zhuǎn)換工具(腳本之家加強(qiáng)版):
http://tools.jb51.net/aideddesign/jb51_paiban
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python time.strptime格式化實(shí)例詳解
在本篇文章里小編給大家整理的是一篇關(guān)于python time.strptime格式化實(shí)例詳解內(nèi)容,對(duì)此有興趣的朋友們可以學(xué)習(xí)參考下。2021-02-02python的paramiko模塊實(shí)現(xiàn)遠(yuǎn)程控制和傳輸示例
本篇文章主要介紹了python的paramiko模塊實(shí)現(xiàn)遠(yuǎn)程控制和傳輸示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10Python實(shí)現(xiàn)獲取磁盤剩余空間的2種方法
這篇文章主要介紹了Python實(shí)現(xiàn)獲取磁盤剩余空間的2種方法,結(jié)合具體實(shí)例形式分析了Python操作計(jì)算機(jī)硬件的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06python利用pytesseract 實(shí)現(xiàn)本地識(shí)別圖片文字
這篇文章主要介紹了python利用pytesseract 實(shí)現(xiàn)本地識(shí)別圖片文字,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12