使用pypinyin批量将全部学生的中文姓名转换为拼音
详细的中文转拼音可查看:https://blog.csdn.net/qq_45708377/article/details/112723390
转载自:https://blog.csdn.net/pythonlaodi/article/details/110128527?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control
import pypinyin
import pandas as pd
df = pd.read_excel('学生名单_test.xls')
df.head()
先导入一份学生名单如下
下面是使用pypinyin批量将全部学生的中文姓名转换为拼音
pinyin_name = []
first_pinyin = []
for i in df['姓名']:
result = pypinyin.pinyin(i, style=pypinyin.NORMAL)
result_ = [i[0] for i in result]
result2 = result_[0].capitalize() + ' ' + ''.join(result_[1:]).capitalize()
result3 = ''.join([i[0].upper() for i in result_])
print(result2, i, sep=' ')
pinyin_name.append(result2)
first_pinyin.append(result3)
df['英文名'] = pinyin_name
df['拼音首字母'] = first_pinyin
df.head()
或者是转换为拼音首字母,运行结果如下: