程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Conversion method of simplified and traditional Chinese characters in python

編輯:Python

python's zhconv library provides the maximum forward matching simplified and traditional conversion based on the MediaWiki vocabulary. Both pyhton2 and python3 support it, which can meet simple conversion needs.
The following summarizes the method of zhconv from installation to use (python3).
First, zhconv installation
The zhconv library can be installed directly using pip, the installation command:

pip install zhconv


Second, how to use zhconv
zhconv supports the conversion of the following regional words:
zh-cn Mainland Simplified
zh-sg Malaysia-Singapore Simplified (Simplified Chinese characters used in Malaysia and Singapore)
zh-tw Taiwanese (Taiwanese)
zh-hk Hong Kong Traditional (Hong Kong Traditional)
zh-hans Simplified
zh-hant Traditional (Traditional)

Method 1: Import zhconv directly

import zhconv# Simplified and Traditional Conversions = 'In front of the Dou Tan, the mountains and the moon are secluded, and Shixiong has dreams and sorrows.When Zhanger looks at Nanxue, Meihua and I have two white heads.'s1 = zhconv.convert(s, 'zh-hant')s2 = zhconv.convert(s, 'zh-tw')s3 = zhconv.convert(s, 'zh-hk')print('Convert to traditional results:', s1)print('Convert to Taiwanese text:', s2)print('Convert to Hong Kong Traditional Results:', s3)

Transformed into traditional Chinese and the result: The moon is secluded in the mountains in front of the Dou Tan, and Shixiong has a dream and a clear sorrow.When Zhanger looks at Nanxue, Meihua and I have two white heads.
Translated into Taiwanese orthodox results: the mountains and moons are secluded in front of the Chaodou Temple, and Shixiong's dreams are born of pure sorrow.When Zhanger looks at Nanxue, Meihua and I have two white heads.
Translated into Hong Kong Traditional Result: The mountains and the moon are secluded in front of the Chaodou Altar, and Shixiong has a dream and a clear sorrow.When Zhanger looks at Nanxue, Meihua and I have two white heads.

Using Method 2: Import zhconv's convert

from zhconv import converts = 'No one accompanies me Gu Xingchen, no one wakes me up and my tea is cold.'s1 = convert(s, 'zh-hant')print('Convert to traditional results:', s1)

Translated into traditional Chinese results: No one accompanies me Gu Xingchen, no one wakes me up and my tea is cold.

—end—


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved