python 实现汉字转拼音

python 简单实现姓名汉字转拼音

eg:
张三丰 ——> zhangsf
郭靖 ——> guojing

#coding=utf-8
from pinyin import PinYin
test = PinYin()
test.load_word()

with open('xingming.txt','r') as content:
    for i in content:
        name=i.rstrip()
        namelist = test.hanzi2pinyin(string=name)
        #print namelist
        if len(namelist)==2:
            print(namelist[0] + namelist[1][0]+"@xx.cn")
        elif len(namelist)==3:
            print(namelist[0]+namelist[1][0] + namelist[2][0]+"@xx.cn")
        else:
            pass

你可能感兴趣的:(python 实现汉字转拼音)