Python

来源:互联网 发布:win7软件打不开 编辑:程序博客网 时间:2024/05/21 09:59

1.打印所有汉字及其拼音

import pypinyinn = 0for i in range(0x4e00,0x9fa6):    print(chr(i),'%-5s'%pypinyin.pinyin(chr(i))[0][0],end = ' ')    n += 1    if (n%15 == 0):        print()

2.汉字拼音的相互转换

from pypinyin import pinyin,lazy_pinyinimport pypinyin#输入汉字,查询同音字def samechr():    in_chr = input("Please input a chiness character:")    n = 0    for i in range(0x4e00,0x9fa6):        if(pinyin(chr(i))[0][0] == pinyin(in_chr)[0][0]):            print(chr(i),end = ' ')            n += 1            if (n%15 == 0):                print()#输入拼音,查询同音字                def sametune():    in_pinyin = input("Please input a PinYin(e.g. zhong1):")    n = 0    for i in range(0x4e00,0x9fa6):        if(pinyin(chr(i), style = pypinyin.TONE3)[0][0] == in_pinyin):            print(chr(i),end = ' ')            n += 1            if (n%15 == 0):                print()#输入字符串,注音def stringtune():    in_str = input("Please input your string:")    py = lazy_pinyin(in_str,style = pypinyin.TONE)    result = ''    for i in range(len(in_str)):        result = result + in_str[i]+ py[i]    print(result)def main():    while True:        print()        print("以字查字1;以音查字2;为字符串注音3;退出99")        choice = input("Please input your choice:")        if (choice == '1'):            samechr()        elif(choice == '2'):            sametune()        elif(choice == '3'):            stringtune()        elif(choice == '99'):            break        else:            print("Please input right choice!!")if __name__ == '__main__':          main()


3.相关参考

汉字拼音转换工具文档:http://pypinyin.mozillazg.com/zh_CN/master/index.html

Python汉字转换成拼音:http://www.cnblogs.com/code123-cc/p/4822886.html

原创粉丝点击