Python 返回漢字的漢語(yǔ)拼音
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
//加載資源文件,將內(nèi)容賦值給 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
//加載資源文件,將內(nèi)容賦值給 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
這里需要用到一個(gè)資源文件。隨后我會(huì)把資源文件地址發(fā)上來(lái)。
我把他改成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
# ?、俸廷谥g的內(nèi)容
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'請(qǐng)輸入漢字: ')
print GetPy(word).lower()
如果大家有什么問(wèn)題歡迎討論。。。。。。
相關(guān)文章
Python釘釘報(bào)警及Zabbix集成釘釘報(bào)警的示例代碼
這篇文章主要介紹了Python釘釘報(bào)警及Zabbix集成釘釘報(bào)警的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Python機(jī)器學(xué)習(xí)算法之決策樹(shù)算法的實(shí)現(xiàn)與優(yōu)缺點(diǎn)
決策樹(shù)(Decision Tree)是一種基本的分類與回歸方法,這篇文章主要給大家介紹了關(guān)于Python機(jī)器學(xué)習(xí)算法之決策樹(shù)算法實(shí)現(xiàn)與優(yōu)缺點(diǎn)的相關(guān)資料,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Python二維數(shù)組不同初始化方式的差異說(shuō)明
這篇文章主要介紹了Python二維數(shù)組不同初始化方式的差異說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08python中nan與inf轉(zhuǎn)為特定數(shù)字方法示例
這篇文章主要給大家介紹了將python中nan與inf轉(zhuǎn)為特定數(shù)字的方法,文中給出了詳細(xì)的示例代碼和運(yùn)行結(jié)果,對(duì)大家的理解和學(xué)習(xí)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05django rest framework之請(qǐng)求與響應(yīng)(詳解)
下面小編就為大家?guī)?lái)一篇django rest framework之請(qǐng)求與響應(yīng)(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望對(duì)大家有所幫助2017-11-11python實(shí)現(xiàn)對(duì)圖片進(jìn)行旋轉(zhuǎn),放縮,裁剪的功能
今天小編就為大家分享一篇python實(shí)現(xiàn)對(duì)圖片進(jìn)行旋轉(zhuǎn),放縮,裁剪的功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08python使用for循環(huán)計(jì)算0-100的整數(shù)的和方法
今天小編就為大家分享一篇python使用for循環(huán)計(jì)算0-100的整數(shù)的和方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02Python實(shí)現(xiàn)獲取網(wǎng)站PR及百度權(quán)重
這篇文章主要介紹了Python實(shí)現(xiàn)獲取網(wǎng)站PR及百度權(quán)重,本文使用傳參的方式請(qǐng)求站長(zhǎng)工具和谷歌工具獲取PR值和百度權(quán)重,需要的朋友可以參考下2015-01-01python編程使用selenium模擬登陸淘寶實(shí)例代碼
這篇文章主要介紹了python編程使用selenium模擬登陸淘寶實(shí)例代碼,涉及selenium的簡(jiǎn)介及Windows下的安裝,分享了相關(guān)代碼示例,小編覺(jué)得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01