Python 返回汉字的汉语拼音
程序员文章站
2022-11-24 09:28:27
后来想到自己delphi有一个获得拼音的代码。于是找了出来。研究了一下代码如下:复制代码 代码如下:function get_hz_pywb(hzstr: string;...
后来想到自己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()
如果大家有什么问题欢迎讨论。。。。。。
复制代码 代码如下:
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()
如果大家有什么问题欢迎讨论。。。。。。
上一篇: gif动态图片:百变的妹子,跳舞,丰胸样样有绝活!
下一篇: python爬虫教程:爬取酷狗音乐
推荐阅读
-
Python中使用items()方法返回字典元素对的教程
-
Python 包含汉字的文件读写之每行末尾加上特定字符
-
Python Sqlite3以字典形式返回查询结果的实现方法
-
Python Sqlite3以字典形式返回查询结果的实现方法
-
详解Python操作RabbitMQ服务器消息队列的远程结果返回
-
Python简单调用MySQL存储过程并获得返回值的方法
-
对Python生成汉字字库文字,以及转换为文字图片的实例详解
-
完美解决Python matplotlib绘图时汉字显示不正常的问题
-
python 实现返回一个列表中出现次数最多的元素方法
-
详解Python操作RabbitMQ服务器消息队列的远程结果返回