实现姓名汉字转成拼音

收藏下,汉字转成拼音,以备不时之需。

eg:
张三丰 —> zhangsf
吴京 —> wujing

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

from xpinyin import Pinyin

with open('1.txt', encoding='utf8') as fp:
# print(fp.read())
word_list = fp.readlines()
for word in word_list:
name = word.strip()
pin = Pinyin()
name_list = pin.get_pinyin(name).split('-')
# print(name_list)
if len(name_list) > 2:
# print(name_list, len(name_list))
name = name_list[0]
for i in range(len(name_list)-1):
name += name_list[i+1][0]
else:
name = ''.join(name_list)
print(name)
坚持原创技术分享,您的支持将鼓励我继续创作!