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的Template使用指南
Sep 11 Python
python实现词法分析器
Jan 31 Python
Python3.5内置模块之os模块、sys模块、shutil模块用法实例分析
Apr 27 Python
如何使用Python实现斐波那契数列
Jul 02 Python
Python实现语音识别和语音合成功能
Sep 20 Python
Django框架安装方法图文详解
Nov 04 Python
浅谈tensorflow中Dataset图片的批量读取及维度的操作详解
Jan 20 Python
matplotlib 对坐标的控制,加图例注释的操作
Apr 17 Python
浅谈Tensorflow加载Vgg预训练模型的几个注意事项
May 26 Python
简单了解Python多态与属性运行原理
Jun 15 Python
python中怎么表示空值
Jun 19 Python
Python  lambda匿名函数和三元运算符
Apr 19 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正则表达式替换站点关键字链接后空白的解决方法
2014/09/16 PHP
php获取汉字拼音首字母的方法
2015/10/21 PHP
javascript 禁止复制网页
2009/06/11 Javascript
原生js仿jq判断当前浏览器是否为ie,精确到ie6~8
2014/08/30 Javascript
JavaScript设计模式之工厂模式和构造器模式
2015/02/11 Javascript
浅谈js基本数据类型和typeof
2016/08/09 Javascript
手机Web APP如何实现分享多平台功能
2016/08/19 Javascript
JS判断输入的字符串是否是数字的方法(正则表达式)
2016/11/29 Javascript
javascript中call,apply,bind函数用法示例
2016/12/19 Javascript
JavaScript中日常收集常见的10种错误(推荐)
2017/01/08 Javascript
微信小程序图表插件(wx-charts)实例代码
2017/01/17 Javascript
微信页面弹出键盘后iframe内容变空白的解决方案
2017/09/20 Javascript
使用React手写一个对话框或模态框的方法示例
2019/04/25 Javascript
在博客园博文中添加自定义右键菜单的方法详解
2020/02/05 Javascript
浅谈Vue2.4.0 $attrs与inheritAttrs的具体使用
2020/03/08 Javascript
从源码角度来回答keep-alive组件的缓存原理
2021/01/18 Javascript
Python函数式编程
2017/07/20 Python
从头学Python之编写可执行的.py文件
2017/11/28 Python
Python中分支语句与循环语句实例详解
2018/09/13 Python
在PyCharm中三步完成PyPy解释器的配置的方法
2018/10/29 Python
Python函数的参数常见分类与用法实例详解
2019/03/30 Python
使用python实现离散时间傅里叶变换的方法
2019/09/02 Python
pytorch加载自定义网络权重的实现
2020/01/07 Python
使用Django和Postgres进行全文搜索的实例代码
2020/02/13 Python
Python+PyQt5实现灭霸响指功能
2020/05/25 Python
Python-split()函数实例用法讲解
2020/12/18 Python
亚马逊西班牙购物网站:amazon西班牙
2017/03/06 全球购物
Vrbo英国:预订度假屋
2020/08/19 全球购物
大一自我鉴定范文
2013/10/04 职场文书
报社实习生自荐信
2014/01/24 职场文书
微观物理专业自荐信
2014/01/26 职场文书
投标服务承诺书
2014/05/28 职场文书
英语演讲开场白
2015/05/29 职场文书
党员干部学习十八届五中全会精神心得体会
2016/01/05 职场文书
学习型家庭事迹材料(2016精选版)
2016/02/29 职场文书
MySQL慢查询的坑
2021/04/28 MySQL