pypinyin--python 汉字与拼音的转换

安装pypinyin

pip install pypinyin / pip3 install pypinyin

使用

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

1. 启用多音字模式

 >>> pinyin(u'单个', heteronym=True)
    [['dān', 'chán', 'shàn'], ['gè', 'gàn', 'gě']]

2. 不考虑多音字的情况

 >>> pinyin(u'单个',heteronym=False)
 [['dān'], ['gè']]
 
 >>> pinyin(u'单个')
 [['dān'], ['gè']]
 
 >>> lazy_pinyin(u'单个')
 ['dan', 'ge']

3. style设置拼音音调风格

from pypinyin import pinyin, Style

(1)Style.TONE3音调显示在末尾

>>> pinyin('单个',style=Style.TONE3)
  [['dan1'], ['ge4']]#数字为音调
    
  >>> pinyin('单个',style=Style.TONE3,heteronym=True)
  [['dan1', 'chan2', 'shan4'], ['ge4', 'gan4', 'ge3']]#数字为音调

(2) Style.TONE2音调显示在韵母

>>> pinyin('单个',style=Style.TONE2,heteronym=True)
  [['da1n', 'cha2n', 'sha4n'], ['ge4', 'ga4n', 'ge3']]#数字为音调

(3)Style.TONE音调为手写格式

>>> pinyin('单个',style=Style.TONE)
 [['dān'], ['gè']]

>>> pinyin('单个',style=Style.TONE,heteronym=True)
 [['dān', 'chán', 'shàn'], ['gè', 'gàn', 'gě']]

(4)pypinyin.NORMAL不显示音调

>>> pinyin(u'单个', heteronym=True,style=pypinyin.NORMAL)
 [['dan', 'chan', 'shan'], ['ge', 'gan']]

>>> pinyin(u'单个', style=pypinyin.NORMAL)
 [['dan'], ['ge']]

你可能感兴趣的:(python)