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判断某个用户对某个文件的权限
Oct 13 Python
Python实现的异步代理爬虫及代理池
Mar 17 Python
基于hashlib模块--加密(详解)
Jun 21 Python
windows10下python3.5 pip3安装图文教程
Apr 02 Python
用 Python 连接 MySQL 的几种方式详解
Apr 04 Python
Python cv2 图像自适应灰度直方图均衡化处理方法
Dec 07 Python
Python使用dict.fromkeys()快速生成一个字典示例
Apr 24 Python
python 调试冷知识(小结)
Nov 11 Python
Django重设Admin密码过程解析
Feb 10 Python
如何基于python3和Vue实现AES数据加密
Mar 27 Python
keras小技巧——获取某一个网络层的输出方式
May 23 Python
基于 Python 实践感知器分类算法
Jan 07 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实现更改hosts文件的方法示例
2017/08/08 PHP
用javascript getComputedStyle获取和设置style的原理
2008/10/10 Javascript
兼容ie、firefox的图片自动缩放的css跟js代码分享
2012/01/21 Javascript
用显卡加速,轻松把笔记本打造成取暖器的办法!
2013/04/17 Javascript
判断javascript的数据类型(示例代码)
2013/12/11 Javascript
邮箱下拉自动填充选择示例代码附图
2014/04/03 Javascript
Bootstrap的fileinput插件实现多文件上传的方法
2016/09/05 Javascript
移动端滑动插件Swipe教程
2016/10/16 Javascript
js+canvas实现动态吃豆人效果
2017/03/22 Javascript
深入nodejs中流(stream)的理解
2017/03/27 NodeJs
EasyUI在Panel上动态添加LinkButton按钮
2017/08/11 Javascript
BootStrap 页签切换失效的解决方法
2017/08/17 Javascript
elementUI select组件value值注意事项详解
2019/05/29 Javascript
vue实现的多页面项目如何优化打包的步骤详解
2020/07/19 Javascript
vue自动添加浏览器兼容前后缀操作
2020/08/13 Javascript
[00:16]热血竞技场
2019/03/06 DOTA
Python实现返回数组中第i小元素的方法示例
2017/12/04 Python
Python实现字符串的逆序 C++字符串逆序算法
2020/05/28 Python
python爬虫selenium和phantomJs使用方法解析
2019/08/08 Python
pytorch自定义二值化网络层方式
2020/01/07 Python
对tensorflow 中tile函数的使用详解
2020/02/07 Python
解决numpy矩阵相减出现的负值自动转正值的问题
2020/06/03 Python
Python3+Flask安装使用教程详解
2021/02/16 Python
很酷的HTML5电子书翻页动画特效
2016/02/25 HTML / CSS
HTML5 拖放(Drag 和 Drop)详解与实例代码
2017/09/14 HTML / CSS
ZWILLING双立人法国网上商店:德国刀具锅具厨具品牌
2019/08/28 全球购物
装潢设计专业推荐信模板
2013/11/26 职场文书
运动会100米解说词
2014/01/23 职场文书
在职党员进社区活动总结
2014/07/05 职场文书
五四青年节的活动方案
2014/08/20 职场文书
奉献家乡演讲稿
2014/09/13 职场文书
2014年房地产工作总结范文
2014/11/19 职场文书
公务员考察材料范文
2014/12/23 职场文书
单位委托函范文
2015/01/29 职场文书
MySQL 那些常见的错误设计规范,你都知道吗
2021/07/16 MySQL
《传颂之物 虚伪的假面》BD发售宣传CM公开
2022/04/04 日漫