1. 程式人生 > 其它 >python的xpinyin模組:漢字轉拼音

python的xpinyin模組:漢字轉拼音

pypinyin

1、安裝

pip install pypinyin

2、使用方法

>>> from pypinyin import pinyin, lazy_pinyin
>>> import pypinyin

>>> pinyin(u'中心')
[[u'zh\u014dng'], [u'x\u012bn']]
# 啟用多音字模式
>>> pinyin(u'中心', heteronym=True)  
[[u'zh\u014dng', u'zh\xf2ng'], [u'x\u012bn']]
# 設定拼音風格
>>> pinyin(u'
中心', style=pypinyin.INITIALS) [['zh'], ['x']] >>> pinyin('中心', style=pypinyin.TONE2, heteronym=True) [['zho1ng', 'zho4ng'], ['xi1n']] # 不考慮多音字的情況 >>> lazy_pinyin(u'中心') ['zhong', 'xin']

xpinyin

1、安裝(或者pip3 install xpinyin 就可以了)

pip install xpinyin

2、使用方法

>>> from xpinyin import
Pinyin >>> >>> p.get_pinyin(u"上海") 'shang-hai' >>> # 顯示聲調 >>> p.get_pinyin(u"上海", show_tone_marks=True) 'shàng-hǎi' >>> # 設定分隔符 >>> p.get_pinyin(u"上海", ' ') 'shang hai' >>> # 只顯示聲母 >>> p.get_initial(u"") 'S' >>> # 顯示多字聲母,並設定分隔符
>>> p.get_initials(u"上海", u'') 'SH'
每天一點點,感受自己存在的意義。