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程序实例(通讯录)
Nov 29 Python
pyqt4教程之实现半透明的天气预报界面示例
Mar 02 Python
Python函数可变参数定义及其参数传递方式实例详解
May 25 Python
Python字符串转换成浮点数函数分享
Jul 24 Python
解决pyqt中ui编译成窗体.py中文乱码的问题
Dec 23 Python
详解Python3.6安装psutil模块和功能简介
May 30 Python
将string类型的数据类型转换为spark rdd时报错的解决方法
Feb 18 Python
Opencv-Python图像透视变换cv2.warpPerspective的示例
Apr 11 Python
Python数组并集交集补集代码实例
Feb 18 Python
Python run()函数和start()函数的比较和差别介绍
May 03 Python
Python字符串及文本模式方法详解
Sep 10 Python
python模拟点击玩游戏的实例讲解
Nov 26 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
让这部DC动画新作刷新你的认知
2020/03/03 欧美动漫
php $_ENV为空的原因分析
2009/06/01 PHP
PHP中定义数组常量(array常量)的方法
2014/11/17 PHP
PHP长连接实现与使用方法详解
2018/02/11 PHP
javascript实现的基于金山词霸网络翻译的代码
2010/01/15 Javascript
jQuery.Validate 使用笔记(jQuery Validation范例 )
2010/06/25 Javascript
jquery加载页面的方法(页面加载完成就执行)
2011/06/21 Javascript
前台js改变Session的值(用ajax实现)
2012/12/28 Javascript
js如何判断不同系统的浏览器类型
2013/10/28 Javascript
JQuery打造省市下拉框联动效果
2014/05/18 Javascript
js实现下拉列表选中某个值的方法(3种方法)
2015/12/17 Javascript
javascript先序遍历DOM树的方法
2016/02/27 Javascript
jQuery动态加载css文件实现方法
2016/06/15 Javascript
ES6入门教程之变量的解构赋值详解
2019/04/13 Javascript
JS中的一些常用的函数式编程术语
2019/06/15 Javascript
python实现linux服务器批量修改密码并生成execl
2014/04/22 Python
跟老齐学Python之不要红头文件(1)
2014/09/28 Python
Python自动连接ssh的方法
2015/03/07 Python
在Django的模板中使用认证数据的方法
2015/07/23 Python
python使用super()出现错误解决办法
2017/08/14 Python
解决Python运行文件出现out of memory框的问题
2018/12/03 Python
libreoffice python 操作word及excel文档的方法
2019/07/04 Python
Python pandas.DataFrame 找出有空值的行
2019/09/09 Python
window7下的python2.7版本和python3.5版本的opencv-python安装过程
2019/10/24 Python
Django框架HttpRequest对象用法实例分析
2019/11/01 Python
Java文件与类动手动脑实例详解
2019/11/10 Python
python pip安装包出现:Failed building wheel for xxx错误的解决
2019/12/25 Python
PyCharm设置Ipython交互环境和宏快捷键进行数据分析图文详解
2020/04/23 Python
python爬取微博评论的实例讲解
2021/01/15 Python
德国领先的大尺码和超大尺码男装在线零售商:Bigtex
2019/06/22 全球购物
仓库管理专业个人的自我评价
2013/12/30 职场文书
信用卡逾期证明示例
2014/09/13 职场文书
2014法院四风问题对照检查材料思想汇报
2014/10/04 职场文书
先进党组织事迹材料
2014/12/26 职场文书
2015公务员年度考核评语
2015/03/25 职场文书
导游词之泉州崇武古城
2019/12/20 职场文书