惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园 - 叶小钗
小众软件
小众软件
WordPress大学
WordPress大学
I
InfoQ
Last Week in AI
Last Week in AI
Vercel News
Vercel News
博客园 - Franky
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta
腾讯CDC
D
DataBreaches.Net
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
Jina AI
Jina AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
G
Google Developers Blog
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell

博客园 - StudyNLP

算法结束篇 贪心-钓鱼问题 贪心-放置雷达 贪心-Stall Reservations 贪心-电影节 贪心-圣诞老人的礼物 广度优先搜索-八数码问题 广度优先搜索-鸣人和佐助 广度优先搜索-迷宫问题 广度优先搜索-抓住那头牛 深度优先搜索-生日蛋糕 深度优先搜索-Roads 深度优先搜索-踩方格 深度优先搜索-城堡问题 动态规划-分蛋糕V2 动态规划-分蛋糕V1 动态规划-滑雪 动态规划-0-1背包问题 动态规划-神奇的口袋V2
python把中文汉字转拼音,存储到excel表格
StudyNLP · 2021-03-07 · via 博客园 - StudyNLP

     汉字转拼音需要下载pypinyin第三方的包,百度搜索一下即可下载。

    有原始的test.xlsx原始数据如下图:

   

   最后新生成一个test.xls文件,带拼音的,姓名是拼音的全称,后面都是取首字母,结果如下图:

  

  经过测试,对部分多音字处理有问题,比如覃,生成Tan了。  

Python实现代码如下:

 1 from pypinyin import Style, lazy_pinyin
 2 from xlutils.copy import copy
 3 import xlrd
 4 
 5 
 6 def get_NamePY(str_data):
 7     rtn = ''
 8     for i in range(len(str_data)):
 9         if i == 0:
10             rtn = lazy_pinyin(str_data[i], style=Style.NORMAL)
11         else:
12             rtn += lazy_pinyin(str_data[i], style=Style.FIRST_LETTER)
13     # ['zhong','g'] join把列表拼接成字符串,capitalize()后面是把首字母转化成大写
14     rtn = ''.join(rtn)
15     return rtn
16 
17 
18 rb = xlrd.open_workbook('test.xlsx')
19 r_sheet = rb.sheet_by_index(0)
20 wb = copy(rb)
21 w_sheet = wb.get_sheet(0)
22 
23 for row_index in range(1, r_sheet.nrows):
24     row = r_sheet.row_values(row_index)
25     row[1] = get_NamePY(row[0])
26     print(row)
27     w_sheet.write(row_index, 1, row[1])
28 
29 wb.save('test.xls')