赞
踩
茕茕孑立 沆瀣一气
踽踽独行 醍醐灌顶
绵绵瓜瓞 奉为圭臬
龙行龘龘 犄角旮旯
娉婷袅娜 涕泗滂沱
呶呶不休 不稂不莠
这首《生僻字》,考验的是“语文”硬实力。倘若实力不够,其实可以拿Python来凑,毕竟库多!今天就来说一个有意思的pypinyin库,名字很直白,是python版的拼音库,用来将汉字转为拼音。
先来看下代码运行效果:
当然,正规的操作是读取汉字txt文本,通过代码生成注音txt文本:
很简单,拼音库可以把汉字转化为拼音,那我们要做的就是读取文本,获取拼音,最后将文本和拼音重新拼接。高深的算法没有,基本围绕字符串和列表的操作拼接
首先是安装,pip install pypinyin
装好后引入模块from pypinyin import pinyin
调用 pinyin(“生僻字”)
,即可得到拼音列表[['shēng'], ['pì'], ['zì']]
基本是入门阶段读写txt文件的简单应用:
txt_name = "生僻字"
with open("%s.txt"%txt_name,encoding="UTF-8-sig") as f:
content = f.read()
input_str = content
result = transfer(input_str)
print(result)
with open("%s_拼音.txt"%txt_name,'w',encoding='UTF-8-sig') as m:
m.write(result)
首先会对读入的文本进行整理,去除多余的空格,将字符串中的汉字与列表中的拼音一一对应并拼接。
由于中文字符占两个字节,以及读写txt时会涉及到编码格式,这部分代码比较费功夫,只能说勉强做到格式整齐吧。
def transfer(text): #去除字符串中空格 no_space_text = ''.join(text.split(" ")) raw_list = [] temp = "" for i in range(len(no_space_text)): if is_Chinese(no_space_text[i]): if temp != "": raw_list.append(temp) temp="" raw_list.append(no_space_text[i]) else: temp+=no_space_text[i] #生成拼音列表 pylist = pinyin(no_space_text) #将拼音列表和字符串拼接对齐 text_list = [] pinyin_list = [] for i in range(len(raw_list)): if is_Chinese(raw_list[i]): pinyin_list.append(pylist[i][0].ljust(6, ' ')) text_list.append(raw_list[i].ljust(5,' ')) else: pinyin_list.append(pylist[i][0]) text_list.append(raw_list[i]) pinyin_str = ''.join(pinyin_list) text_str = ''.join(text_list) pinyin_line_list = pinyin_str.split('\n') text_line_list = text_str.split("\n") final_result = "" for i in range(len(pinyin_line_list)): final_result += pinyin_line_list[i] + '\n' + text_line_list[i] + "\n" return final_result
最初接触一个读取剪贴板内容的pyperclip模块,琢磨对剪贴板中字符串如何操作。恰巧听到《生僻字》,觉得既然逐字查拼音是个麻烦事,何不通过Python来批量添加拼音呢?很幸运,还真有大佬写了这个pypinyin库,在Github上也已有1500多颗星了~
# -*- coding:utf-8 -*- from pypinyin import pinyin def is_Chinese(word): for ch in word: #通常汉字都在'\u4e00' 和 '\u9fff之间,但是生僻字如“㙓” 位于拓展库 '\u3400' 到 '\u4DB5'之间 if ('\u4e00' <= ch and ch <= '\u9fff') or ('\u3400' <= ch and ch <= '\u4DB5'): return True return False def transfer(text): #去除字符串中空格 no_space_text = ''.join(text.split(" ")) raw_list = [] temp = "" for i in range(len(no_space_text)): if is_Chinese(no_space_text[i]): if temp != "": raw_list.append(temp) temp="" raw_list.append(no_space_text[i]) else: temp+=no_space_text[i] #生成拼音列表 pylist = pinyin(no_space_text) #将拼音列表和字符串拼接对齐 text_list = [] pinyin_list = [] for i in range(len(raw_list)): if is_Chinese(raw_list[i]): pinyin_list.append(pylist[i][0].ljust(6, ' ')) text_list.append(raw_list[i].ljust(5,' ')) else: pinyin_list.append(pylist[i][0]) text_list.append(raw_list[i]) pinyin_str = ''.join(pinyin_list) text_str = ''.join(text_list) pinyin_line_list = pinyin_str.split('\n') text_line_list = text_str.split("\n") final_result = "" for i in range(len(pinyin_line_list)): final_result += pinyin_line_list[i] + '\n' + text_line_list[i] + "\n" return final_result def main(): txt_name = "生僻字" #读写txt文档时,encoding="UTF-8-sig"是为了去除utf-8带的BOM头'\ufeff' with open("%s.txt"%txt_name,encoding="UTF-8-sig") as f: content = f.read() input_str = content # print('input_str:',input_str) result = transfer(input_str) print(result) with open("%s_拼音.txt"%txt_name,'w',encoding='UTF-8-sig') as m: m.write(result) if __name__ == '__main__': main()
代码中有两点,在这记录下:
参考:https://zhuanlan.zhihu.com/p/55578646
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。