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创建声明性迷你语言的教程
Apr 13 Python
使用rst2pdf实现将sphinx生成PDF
Jun 07 Python
浅谈python中的变量默认是什么类型
Sep 11 Python
对pandas中apply函数的用法详解
Apr 10 Python
python线程中同步锁详解
Apr 27 Python
Python使用wget实现下载网络文件功能示例
May 31 Python
python-opencv 将连续图片写成视频格式的方法
Jan 08 Python
在Pycharm terminal中字体大小设置的方法
Jan 16 Python
numpy数组做图片拼接的实现(concatenate、vstack、hstack)
Nov 08 Python
Python 字符串池化的前提
Jul 03 Python
总结Pyinstaller打包的高级用法
Jun 28 Python
关于Python OS模块常用文件/目录函数详解
Jul 01 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中的Session和Cookie
2013/06/21 PHP
php中strlen和mb_strlen用法实例分析
2016/11/12 PHP
jquery事件机制扩展插件 jquery鼠标右键事件
2011/12/21 Javascript
jQuery时间轴插件使用详解
2015/07/16 Javascript
JavaScript中获取纯正的undefined的方法
2016/03/06 Javascript
js实现多图左右切换功能
2016/08/04 Javascript
AngularJS  $on、$emit和$broadcast的使用
2016/09/05 Javascript
详解Vue组件之间的数据通信实例
2017/06/17 Javascript
angular的输入和输出的使用方法
2018/09/22 Javascript
JQuery判断radio单选框是否选中并获取值的方法
2019/01/17 jQuery
js中事件对象和事件委托的介绍
2019/01/21 Javascript
详解基于webpack&amp;gettext的前端多语言方案
2019/01/29 Javascript
浅谈对于react-thunk中间件的简单理解
2019/05/01 Javascript
如何优雅地取消 JavaScript 异步任务
2020/03/22 Javascript
基于Web Audio API实现音频可视化效果
2020/06/12 Javascript
Vue记住滚动条和实现下拉加载的完美方法
2020/07/31 Javascript
用JavaScript实现贪吃蛇游戏
2020/10/23 Javascript
[01:59]深扒TI7聊天轮盘语音出处 1
2017/05/11 DOTA
python双向链表实现实例代码
2013/11/21 Python
简单介绍Python中的filter和lambda函数的使用
2015/04/07 Python
python中利用h5py模块读取h5文件中的主键方法
2018/06/05 Python
redis之django-redis的简单缓存使用
2018/06/07 Python
Python程序打包工具py2exe和PyInstaller详解
2019/06/28 Python
Python(PyS60)实现简单语音整点报时
2019/11/18 Python
解决Pycharm 导入其他文件夹源码的2种方法
2020/02/12 Python
Python datetime 格式化 明天,昨天实例
2020/03/02 Python
使用PyQt5实现图片查看器的示例代码
2020/04/21 Python
Python中Qslider控件实操详解
2021/02/20 Python
iHerb台湾:维生素、保健品和健康产品
2018/01/31 全球购物
给导游的表扬信
2014/01/10 职场文书
初三物理教学反思
2014/01/21 职场文书
教学质量评估实施方案
2014/03/17 职场文书
食品业务员岗位职责
2014/03/18 职场文书
安全口号大全
2014/06/21 职场文书
2015年全国爱眼日活动小结
2015/02/27 职场文书
律政俏佳人观后感
2015/06/09 职场文书