Python3 将汉语转换为汉语拼音
程序员文章站
2024-03-16 14:26:16
...
工具: 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
注:新手上路,本文章仅供参考.