/ Published in: Python
with given key_word searches through a text or web file and indicates number of times that the word is repeated.
Expand |
Embed | Plain Text
# BY: AMIR NAGHAVI # <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> # <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><> """ this two programm go through lines among text or html files and count number of repeating the given word """ class find(object): def __init__(self,path,word): self.p=path #-----text file path self.w=word #-----the word to look for repeating def start(self): f=open(self.p) chrcount=0 # character counter ;indicates and separates words numofrep=0 # number of word that would repeat length=len(self.w) for line in f: lineflag=False #--to printing line once if it has the give word more than once <-- for j in line: # \ if j==self.w[chrcount] and chrcount<=length: # \ chrcount+=1 # \ if chrcount==length: #--finding the word | chrcount=0 # | numofrep+=1 # | if lineflag!=True: # if in line are more than one "word" print line once-| print line lineflag=True else: chrcount=0 print 'Number of repeat: ',numofrep # ------------------------------- searvhing through web pages class word_counter_in_web(find): def __init__(self,add,word): self.add=add self.w=word def start(self): from urllib import urlopen #url=urlopen(self.add) length=len(self.w) chrcount=0 numofrep=0 for line in self.add: lineflag=False for j in line: if j==self.w[chrcount] and chrcount<=length: chrcount+=1 if chrcount==length: numofrep+=1 chrcount=0 if lineflag!=True: lineflag=True #print 'repeat number for %s is %d' %(self.w,numofrep) return numofrep # ------------------------------------- # RUN: if __name__=="__main__": find('D:\cpuz-readme.txt','cpu').start() #print word_counter_in_web('http://www.python.org','python').start()
Comments
Subscribe to comments
You need to login to post a comment.

please run and if you want study code and give your opinion on me.