网络编程
位置:首页>> 网络编程>> Python编程>> Python3实现汉语转换为汉语拼音

Python3实现汉语转换为汉语拼音

作者:r_coder  发布时间:2022-02-28 11:27:54 

标签:python3,汉语,拼音

本文实例为大家分享了Python3实现汉语转换为汉语拼音的具体代码,供大家参考,具体内容如下

工具: Python3.6.2,pycharm

1.使用了 第三方模块 pypinyin(点击File->setting...->Project:name(自己的项目名称)->Project Interpreter)

Python3实现汉语转换为汉语拼音

点击+ ,输入pypinyin,点击 Install Pageage

Python3实现汉语转换为汉语拼音

2. 上代码


import pypinyin

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

# 带声调的(默认)
def hp2(word):
s = ''
for i in pypinyin.pinyin(word):
s = s + ''.join(i) + " "
return s

if __name__ == "__main__":
print(hp("中国中央电视台春节联欢晚会"))
print(hp2("中国中央电视台春节联欢晚会"))

输出结果:


D:\Python\Python36\python.exe D:/pyWorkspace/reptile/chinesePYC.py
zhongguozhongyangdianshitaichunjielianhuanwanhui
zhōng guó zhōng yāng diàn shì tái chūn jié lián huān wǎn huì

Process finished with exit code 0

注:新手上路,本文章仅供参考.

来源:https://blog.csdn.net/r_coder/article/details/79419318

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com