Python之xpinyin模块学习

原由:

昨天晚上跟小B聊天,发现他的头像上写了几个字:WYN喜欢HYJ

八卦心理作祟,想到有他的全校花名单Excel,so想找出这个HYJ还不容易。

昨晚简单搜索了下,excel 将姓名一栏变为汉语拼音(汉字转拼音),然后提取汉语拼音首字母缩写。

在excel要用函数实现,发现有点复杂。放弃。

今天想想,如何拿Python去实现,搜索:python把中文文档变为拼音,内容很多

 

以上参考连接:https://blog.csdn.net/qq_33232071/article/details/50915760

具体实现

import xlrd
from xpinyin import Pinyin

book = xlrd.open_workbook('result.xls')  # 打开一个excel
sheet = book.sheet_by_index(0)  # 根据顺序获取sheet
for i in range(sheet.nrows):
    p = Pinyin()
    # print(p.get_initials(sheet.col_values(0)[i]))
    if p.get_initials(sheet.col_values(0)[i])=="H-Y-J":

        print( sheet.row(i),i+1 )

 执行结果:

[text:'何*洁', text:'1910151019', text:'23', text:'合**中', number:344.5, number:18.0, number:1004.0, number:99.0, number:98.0, number:62.5, number:23.0, number:22.0, number:40.0] 1008

 用到的知识点:

1.excel文件读取:打开文件,读取sheet表,获取表的所有行和列  获取某个单元格的位置(行和列)

2.xpinyin 模块:p.get_initials(u'上海')  SH

原文地址:https://www.cnblogs.com/eosclover/p/10299734.html