python 得到汉字的拼音

import pypinyin
# 不带声调的(style=pypinyin.NORMAL)
def pinyin(word):
    s = ''
    for i in pypinyin.pinyin(word, style=pypinyin.NORMAL):
        s += ''.join(i)
    return s

# 带声调的(默认)
def yinjie(word):
    s = ''
    # heteronym=True开启多音字
    for i in pypinyin.pinyin(word, heteronym=True):
        s = s + ''.join(i) + " "
    return s
print(pinyin('张三'))
print(yinjie('张三'))

结果:

====================== RESTART: D:/python_pandas/pinyin.py =====================
zhangsan
zhāng sān
>>>

posted @ 2021-01-31 14:21  myrj  阅读(1051)  评论(0编辑  收藏  举报