从清华的镜像去安装pypinyin
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pypinyin
from pypinyin import pinyin, Style, lazy_pinyin
def convert_to_pinyin(text):
return ' '.join([sub[0] for sub in pinyin(text, style=Style.TONE2)])
def convert_to_pinyin2(text):
return ' '.join([sub[0] for sub in pinyin(text)])
def convert_to_lazy_pinyin(text):
return ' '.join(lazy_pinyin(text))
text = '''
姓名
孙涵雅
张三
钱浩
唐燕姿
'''
converted_text = convert_to_pinyin(text)
print(converted_text) # nǐ hǎo shì jì
converted_text2 = convert_to_pinyin2(text)
print(converted_text2) # nǐ hǎo shì jì
converted_text = convert_to_lazy_pinyin(text)
print(converted_text )
输出结果
xi4ng mi2ng
su1n ha2n ya3
zha1ng sa1n
qia2n ha4o
ta2ng ya4n zi1
xìng míng
sūn hán yǎ
zhāng sān
qián hào
táng yàn zī
xing ming
sun han ya
zhang san
qian hao
tang yan zi
标签:convert,拼音,python,text,汉字,ng,converted,pinyin,sub
From: https://www.cnblogs.com/danielHan/p/18678607