python实现汉字转拼音
所用模块
模块安装
pip install xpinyin
使用方式
from xpinyin import Pinyin
p = Pinyin()
pinyin = p.get_pinyin("上海") # shang-hai
print(pinyin)
# 显示声调1
pinyin = p.get_pinyin("上海", tone_marks=True) #shang4-hai3
print(pinyin)
# 显示声调
pinyin = p.get_pinyin("上海", tone_marks='marks') #shàng-hǎi
print(pinyin)
# 设置分隔符
pinyin = p.get_pinyin("上海", ' ') # 'shang hai'
print(pinyin)
# 只显示声母
pinyin = p.get_initial("上") # 'S'
print(pinyin)
# 显示多字声母,并设置分隔符
p.get_initials("上海", '') #'SH'
print(pinyin)
shang-hai
shang4-hai3
shàng-hǎi
shang hai
S
S
评论 (0)