Python 返回汉字的汉语拼音


Posted in Python onFebruary 27, 2009

后来想到自己Delphi有一个获得拼音的代码。于是找了出来。研究了一下代码如下:

function get_hz_pywb(hzstr: string; pytype: integer): string; 
var 
I: Integer; 
allstr: string; 
hh: THandle; 
pp: pointer; 
ss: TStringList; 
function retturn_wbpy(tempstr: string; tqtype: integer): string; 
var 
outstr, str: string; 
i: integer; 
begin 
//################### 汉字查询电位 
i := 0; 
while i <= ss.Count - 1 do 
begin 
str := ss.Strings[i]; 
if (tempstr = trim(str[1] + str[2])) or (tempstr = trim(str[3] + str[4])) then 
begin 
str := ss.Strings[i]; 
Break; 
end; 
i := i + 1; 
end; 
//################### 
outstr := ''; //提取编码 
if tqtype = 1 then 
begin 
for i := pos('①', str) + 2 to pos('②', str) - 1 do 
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; 
end; 
if tqtype = 2 then 
begin 
for i := pos('②', str) + 2 to pos('③', str) - 1 do 
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; 
end; 
if tqtype = 3 then 
begin 
for i := pos('③', str) + 2 to pos('④', str) - 1 do 
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; 
end; 
if tqtype = 4 then 
begin 
for i := pos('④', str) + 2 to length(str) do 
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; 
end; 
Result := trim(outstr); 
end; 
begin 
//加载资源文件,将内容赋值给 s 
ss := TStringList.Create; 
hh := FindResource(hInstance, 'mywb', 'TXT'); 
hh := LoadResource(hInstance, hh); 
pp := LockResource(hh); 
ss.Text := pchar(pp); 
UnLockResource(hh); 
FreeResource(hh); 
allstr := ''; 
i := 0; 
while i <= length(hzstr) do //提取汉字字符 
begin 
if (Ord(hzstr[I]) > 127) then 
begin 
if allstr = '' then 
allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype) 
else 
allstr := allstr + retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype); 
i := i + 2; 
end 
else 
begin 
if allstr = '' then allstr := hzstr[I] else allstr := allstr + hzstr[I]; 
i := i + 1; 
end; 
end; 
ss.Free; 
Result := trim(allstr); 
en 
function get_hz_pywb(hzstr: string; pytype: integer): string; 
var 
I: Integer; 
allstr: string; 
hh: THandle; 
pp: pointer; 
ss: TStringList; 
function retturn_wbpy(tempstr: string; tqtype: integer): string; 
var 
outstr, str: string; 
i: integer; 
begin 
//################### 汉字查询电位 
i := 0; 
while i <= ss.Count - 1 do 
begin 
str := ss.Strings[i]; 
if (tempstr = trim(str[1] + str[2])) or (tempstr = trim(str[3] + str[4])) then 
begin 
str := ss.Strings[i]; 
Break; 
end; 
i := i + 1; 
end; 
//################### 
outstr := ''; //提取编码 
if tqtype = 1 then 
begin 
for i := pos('①', str) + 2 to pos('②', str) - 1 do 
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; 
end; 
if tqtype = 2 then 
begin 
for i := pos('②', str) + 2 to pos('③', str) - 1 do 
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; 
end; 
if tqtype = 3 then 
begin 
for i := pos('③', str) + 2 to pos('④', str) - 1 do 
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; 
end; 
if tqtype = 4 then 
begin 
for i := pos('④', str) + 2 to length(str) do 
if str[i] <> '' then if outstr = '' then outstr := str[i] else outstr := outstr + str[i]; 
end; 
Result := trim(outstr); 
end; 
begin 
//加载资源文件,将内容赋值给 s 
ss := TStringList.Create; 
hh := FindResource(hInstance, 'mywb', 'TXT'); 
hh := LoadResource(hInstance, hh); 
pp := LockResource(hh); 
ss.Text := pchar(pp); 
UnLockResource(hh); 
FreeResource(hh); 
allstr := ''; 
i := 0; 
while i <= length(hzstr) do //提取汉字字符 
begin 
if (Ord(hzstr[I]) > 127) then 
begin 
if allstr = '' then 
allstr := retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype) 
else 
allstr := allstr + retturn_wbpy(hzstr[I] + hzstr[I + 1], pytype); 
i := i + 2; 
end 
else 
begin 
if allstr = '' then allstr := hzstr[I] else allstr := allstr + hzstr[I]; 
i := i + 1; 
end; 
end; 
ss.Free; 
Result := trim(allstr); 
en

这里需要用到一个资源文件。随后我会把资源文件地址发上来。
我把他改成Python代码供大家研究。。。。
# -*-coding:utf-8-*- 
# 返回汉字的拼音 
def Return_pinyin(word): 
global reslist 
for line in reslist: 
if (word==line[0]+line[1]) or (word==line[2]+line[3]): 
str = line 
break 
# 取①和②之间的内容 
s = str.find(u'①')+4 
e = str.find(u'②')+3 
return str[s:e] def GetPy(word): 
#首先装载资源文件 
i=0 
allstr = '' 
while i<len(word): 
if ord(word[i])>127: 
if allstr: 
allstr += Return_pinyin(word[i]+word[i+1]) 
else: 
allstr = Return_pinyin(word[i]+word[i+1]) 
i +=2 
else: 
if allstr: 
allstr += word[i] 
else: 
allstr = word[i] 
i +=1 
return allstr 
if __name__=='__main__': 
f = open('wbtext1.txt','r') 
reslist = f.readlines() 
f.close() 
word = raw_input(u'请输入汉字: ') 
print GetPy(word).lower()

如果大家有什么问题欢迎讨论。。。。。。
Python 相关文章推荐
用Python的Django框架来制作一个RSS阅读器
Jul 22 Python
Python的numpy库中将矩阵转换为列表等函数的方法
Apr 04 Python
Python中使用pypdf2合并、分割、加密pdf文件的代码详解
May 21 Python
python使用pandas处理excel文件转为csv文件的方法示例
Jul 18 Python
python 读取修改pcap包的例子
Jul 23 Python
Django Rest framework频率原理与限制
Jul 26 Python
解决pycharm 安装numpy失败的问题
Dec 05 Python
详解python opencv、scikit-image和PIL图像处理库比较
Dec 26 Python
Python如何使用PIL Image制作GIF图片
May 16 Python
selenium设置浏览器为headless无头模式(Chrome和Firefox)
Jan 08 Python
python实现马丁策略回测3000只股票的实例代码
Jan 22 Python
Python按顺序遍历并读取文件夹中文件
Apr 29 Python
用Python的urllib库提交WEB表单
Feb 24 #Python
wxpython 学习笔记 第一天
Feb 09 #Python
python 输出一个两行字符的变量
Feb 05 #Python
Python 调用DLL操作抄表机
Jan 12 #Python
Python MD5文件生成码
Jan 12 #Python
Python Mysql数据库操作 Perl操作Mysql数据库
Jan 12 #Python
Python 学习笔记
Dec 27 #Python
You might like
php array_unique之后json_encode需要注意
2011/01/02 PHP
php中将字符串转为HTML的实体引用的一个类
2013/02/03 PHP
ecshop 2.72如何修改后台访问地址
2015/03/03 PHP
php基于环形链表解决约瑟夫环问题示例
2017/11/07 PHP
Laravel框架Eloquent ORM删除数据操作示例
2019/12/03 PHP
jQuery的.live()和.die() 使用介绍
2011/09/10 Javascript
iframe窗口高度自适应的实现方法
2014/01/08 Javascript
jquery+CSS实现的水平布局多级网页菜单效果
2015/08/24 Javascript
jquery实现鼠标点击后展开列表内容的导航栏效果
2015/09/14 Javascript
javascript时间排序算法实现活动秒杀倒计时效果
2021/01/28 Javascript
BootStrap 附加导航组件
2016/07/22 Javascript
基于JavaScript实现的折半查找算法示例
2017/04/14 Javascript
基于angular6.0实现的一个组件懒加载功能示例
2018/04/12 Javascript
常见的浏览器存储方式(cookie、localStorage、sessionStorage)
2019/05/07 Javascript
详解package.json版本号规则
2019/08/01 Javascript
[01:19:54]DOTA2上海特级锦标赛主赛事日 - 2 败者组第二轮#1Alliance VS EHOME
2016/03/03 DOTA
使用优化器来提升Python程序的执行效率的教程
2015/04/02 Python
Python中datetime模块参考手册
2017/01/13 Python
Python随机生成均匀分布在单位圆内的点代码示例
2017/11/13 Python
Python实现判断一行代码是否为注释的方法
2018/05/23 Python
numpy.random模块用法总结
2019/05/27 Python
python的pytest框架之命令行参数详解(下)
2019/06/27 Python
Python OpenCV 使用滑动条来调整函数参数的方法
2019/07/08 Python
python之yield和Generator深入解析
2019/09/18 Python
基于Python爬取搜狐证券股票过程解析
2020/11/18 Python
Python实现自动整理文件的脚本
2020/12/17 Python
IE10 Error.stack 让脚本调试更加方便快捷
2013/04/22 HTML / CSS
美国美发品牌:Bumble and Bumble
2016/10/08 全球购物
应届优秀本科大学毕业生自我鉴定
2014/01/21 职场文书
幼儿园保教管理制度
2014/02/03 职场文书
药品销售内勤岗位职责
2015/04/13 职场文书
教师节随笔
2015/08/15 职场文书
《女娲补天》读后感5篇
2019/12/31 职场文书
MySQL下使用Inplace和Online方式创建索引的教程
2021/05/26 MySQL
制作能在nginx和IIS中使用的ssl证书
2021/06/21 Servers
Spring Boot接口定义和全局异常统一处理
2022/04/20 Java/Android