日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

python在文件中倒序查找個(gè)關(guān)鍵詞

作者:程序小小小猿 更新時(shí)間: 2022-08-30 編程語(yǔ)言

python在文件中倒序查找個(gè)關(guān)鍵單詞,忽略大小寫來(lái)匹配,并將關(guān)鍵單詞所在行作為結(jié)果返回,以單詞輸入順序作為優(yōu)先度實(shí)現(xiàn)返回(目前只返回第1個(gè)單詞所在行,可實(shí)現(xiàn)多個(gè)返回)

#coding = utf-8

# @author : dj
import re


# the second param 你想要尋找的字母(順序影響結(jié)果,順序即優(yōu)先度)
# 調(diào)用方式: (fname,copyright,source)

def extractOriginalAgency(fname,*dependingList):
count_ = len(dependingList)
result = ["none"] * count_
n =0
with open(fname, 'r', encoding='utf-8') as f:


try:
lines = f.readlines() #讀取所有行
i = 0
while(True):
# find from the ending of the passage

string = lines[i]

for j in range(0,count_):
if(result[j] == "none" ):
if (match(string,dependingList[j])):
# 匹配成功
result[j] =string
n = n+1

# 當(dāng)找到所有單詞是即不必再往下找
if(n == count_):
break

i += 1;
except:
pass

# 找到目標(biāo)所在行后
finResult = "none"
temp = "none"
# 優(yōu)先返回有
for z in range(0,count_):
if(temp =="none" and result[z] !="none" ):
temp = result[z]
if(finResult =="none"):
finResult = temp

return finResult


def match(string,reString):
# 用restring 匹配string
matchObj = re.match(reString,string,re.IGNORECASE)

if(matchObj):
return True
else:
return False


 


原文鏈接:https://blog.csdn.net/m0_67911416/article/details/126593363

欄目分類
最近更新