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中replace方法实例分析
Aug 20 Python
用Python制作简单的朴素基数估计器的教程
Apr 01 Python
Python的MongoDB模块PyMongo操作方法集锦
Jan 05 Python
详解python中executemany和序列的使用方法
Aug 12 Python
Python字典中的键映射多个值的方法(列表或者集合)
Oct 17 Python
python3中类的继承以及self和super的区别详解
Jun 26 Python
Pandas实现dataframe和np.array的相互转换
Nov 30 Python
opencv python Canny边缘提取实现过程解析
Feb 03 Python
python实现可下载音乐的音乐播放器
Feb 25 Python
python 如何利用argparse解析命令行参数
Sep 11 Python
python统计mysql数据量变化并调用接口告警的示例代码
Sep 21 Python
python 网络编程要点总结
Jun 18 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
2009/06/29 PHP
PHP 日,周,月点击排行统计
2012/01/11 PHP
php中使用$_REQUEST需要注意的一个问题
2013/05/02 PHP
php生成二维码
2015/08/10 PHP
PHP数据对象PDO操作技巧小结
2016/09/27 PHP
laravel 5异常错误:FatalErrorException in Handler.php line 38的解决
2017/10/12 PHP
PHP各种常见经典算法总结【排序、查找、翻转等】
2019/08/05 PHP
php给数组赋值的实例方法
2019/09/26 PHP
php装饰者模式简单应用案例分析
2019/10/23 PHP
070823更新的一个[消息提示框]组件 兼容ie7
2007/08/29 Javascript
jquery获取被勾选的checked(选中)的那一行的3列和4列的值
2013/07/04 Javascript
常用的jquery模板插件——jQuery Boilerplate介绍
2014/09/23 Javascript
非jQuery实现照片散落桌子上,单击放大的LightBox效果
2014/11/28 Javascript
jQuery中addClass()方法用法实例
2015/01/05 Javascript
JQuery插入DOM节点的方法
2015/06/11 Javascript
浅谈javascript构造函数与实例化对象
2015/06/22 Javascript
JavaScript数组去重的3种方法和代码实例
2015/07/01 Javascript
jQuery实现下拉框选择图片功能实例
2015/08/08 Javascript
JS实现的不规则TAB选项卡效果代码
2015/09/18 Javascript
JS处理json日期格式化问题
2015/10/01 Javascript
js绘制购物车抛物线动画
2020/11/18 Javascript
node.js 中间件express-session使用详解
2017/05/20 Javascript
jQuery+CSS实现的table表格行列转置功能示例
2018/01/08 jQuery
vue的安装及element组件的安装方法
2018/03/09 Javascript
webpack的tree shaking的实现方法
2019/09/18 Javascript
[00:35]DOTA2上海特级锦标赛 EG战队宣传片
2016/03/04 DOTA
matplotlib设置legend图例代码示例
2017/12/19 Python
python 判断网络连通的实现方法
2018/04/22 Python
Python subprocess模块功能与常见用法实例详解
2018/06/28 Python
python3用PIL把图片转换为RGB图片的实例
2019/07/04 Python
python变量命名的7条建议
2019/07/04 Python
Python 等分切分数据及规则命名的实例代码
2019/08/16 Python
TFRecord文件查看包含的所有Features代码
2020/02/17 Python
python字典key不能是可以是啥类型
2020/08/04 Python
出门问问全球官方商城:Tichome音箱和TicWatch智能手表
2017/12/02 全球购物
Redis数据结构之链表与字典的使用
2021/05/11 Redis