Python汉字转换成拼音
使用场景:创建客户初始账户名,使用公司名拼音首字母
xpinyin 0.5.5
>>> from xpinyin import Pinyin
>>> p = Pinyin()
>>> # default splitter is `-`
>>> p.get_pinyin(u"上海")
'shang-hai'
>>> # show tone marks
>>> p.get_pinyin(u"上海", show_tone_marks=True)
'shàng-hǎi'
>>> # remove splitter
>>> p.get_pinyin(u"上海", '')
'shanghai'
>>> # set splitter as whitespace
>>> p.get_pinyin(u"上海", ' ')
'shang hai'
>>> p.get_initial(u"上")
'S'
>>> p.get_initials(u"上海")
'S-H'
>>> p.get_initials(u"上海", u'')
'SH'
>>> p.get_initials(u"上海", u' ')
'S H'
实例
公司列表取公司名称前六位拼音首字母,替换login
ldf = pd.read_excel(r'c:\Users\NewHopeData\Downloads\res.users.xls')
# 去掉admin账户
lndf=ldf[1:]
namelist =lndf.name.to_list()
# 实例化Pinyin对象
p = Pinyin()
loginlist =[p.get_initials(i[:6],'').lower() for i in namelist]
lndf.login = loginlist
替换login列
网友评论