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

yipeiwu_com7年前Python基础

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

工具: Python3.6.2,pycharm

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

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

对python修改xml文件的节点值方法详解

这是我的xml文件结构 <?xml version='1.0' encoding='utf-8'?> <annotation> <fo...

python实现简易数码时钟

python实现简易数码时钟

最近迷上了Python,要说为什么呢?Python语法简单,功能强大,有广泛的第三方库能快速编程实现自己的想法(无需重复去造轮子)。就像某位前辈说的:“人生苦短,学会偷懒…”,配置好su...

用Python中的turtle模块画图两只小羊方法

用Python中的turtle模块画图两只小羊方法

这两天在一个公众号里看到好玩的turtle库,今天来学习一下。 turtle.circle(radius, extent=None, steps=None) 描述: 以给定半径画圆 参数...

pytorch实现用CNN和LSTM对文本进行分类方式

model.py: #!/usr/bin/python # -*- coding: utf-8 -*- import torch from torch import nn imp...

Python函数中不定长参数的写法

Python函数中不定长参数的写法

1、不定长参数的写法,用 *变量名 表示 2、不定长参数累加 3、不定长参数,使用**c接受m=23,n=56的值; 传参时,a必写,b、c可以缺省 def fun(a, b,...