xpinyin库
一、 简介
将汉字转换为拼音的的第三方库:xpinyin
安装:
pip install xpinyin
导包:
from xpinyin import Pinyin
二、 基本语法
>>> p = Pinyin() # 实例化对象
>>> p.get_pinyin("上海") # 默认的拼音连接是 -
'shang-hai'
>>> p.get_pinyin("上海", tone_marks='marks') # 显示完整的拼音
'shàng-hǎi'
>>> p.get_pinyin("上海", tone_marks='numbers') # 显示第几声
'shang4-hai3'
>>> p.get_pinyin("上海", ' ') # 是的每个拼音之间使用空格分隔
'shang hai'
>>> p.get_initial("上") # 得到一个汉字拼音的大写首字母,注意参数只能是一个汉字
'S'
>>> p.get_initials("上海") # 得到每个汉字拼音的大写首字母
'S-H'
>>> p.get_initials("上海", ' ') # 每个字母之间使用空格分隔
'S H'
>>> p.get_initials("上海", splitter='-', with_retroflex=True) # 显示卷舌音
'SH-H'
>>> p.get_pinyins('模型', splitter=' ', tone_marks='marks') # 显示完整的拼音,同时使用空格分开,同时显示所有可能的音调
['mó xíng', 'mú xíng']