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模擬鼠標(biāo)點(diǎn)擊實(shí)現(xiàn)方法(將通過(guò)實(shí)例自動(dòng)化模擬在360瀏覽器中自動(dòng)搜索python)
這篇文章主要介紹了Python模擬鼠標(biāo)點(diǎn)擊實(shí)現(xiàn)方法(將通過(guò)實(shí)例自動(dòng)化模擬在360瀏覽器中自動(dòng)搜索python),需要的朋友可以參考下2017-08-08
Python利用卡方Chi特征檢驗(yàn)實(shí)現(xiàn)提取關(guān)鍵文本特征
卡方檢驗(yàn)最基本的思想就是通過(guò)觀察實(shí)際值與理論值的偏差來(lái)確定理論的正確與否。本文將利用卡方Chi特征檢驗(yàn)實(shí)現(xiàn)提取關(guān)鍵文本特征功能,感興趣的可以了解一下2022-12-12
Python中的random.choices函數(shù)用法詳解
這篇文章主要給大家介紹了關(guān)于Python中random.choices函數(shù)用法的相關(guān)資料,random.random()?的功能是隨機(jī)返回一個(gè)?0-1范圍內(nèi)的浮點(diǎn)數(shù),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-08-08
Django生成PDF文檔顯示在網(wǎng)頁(yè)上以及解決PDF中文顯示亂碼的問(wèn)題
這篇文章主要介紹了Django生成PDF文檔顯示在網(wǎng)頁(yè)上以及解決PDF中文顯示亂碼的問(wèn)題,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-07-07
初學(xué)者學(xué)習(xí)Python好還是Java好
在本篇文章里小編給大家分享的是關(guān)于初學(xué)者學(xué)習(xí)Python好還是Java好的相關(guān)內(nèi)容,需要的朋友們可以學(xué)習(xí)下。2020-05-05
Python enumerate內(nèi)置庫(kù)用法解析
這篇文章主要介紹了Python enumerate內(nèi)置庫(kù)用法解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
一小時(shí)學(xué)會(huì)TensorFlow2之基本操作2實(shí)例代碼
這篇文章主要介紹了TensorFlow2的基本操作和實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
python列表中remove()函數(shù)的使用方法詳解
這篇文章主要給大家介紹了關(guān)于python列表中remove()函數(shù)的使用,以及Python列表的remove方法的注意事項(xiàng),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
Python中實(shí)現(xiàn)進(jìn)度條的多種方法總結(jié)
在Python編程中,進(jìn)度條是一個(gè)非常有用的功能,它能讓用戶直觀地了解任務(wù)的進(jìn)度,提升用戶體驗(yàn),本文將介紹幾種在Python中實(shí)現(xiàn)進(jìn)度條的常用方法,并通過(guò)代碼示例和案例來(lái)展示它們的具體應(yīng)用,需要的朋友可以參考下2025-01-01

