python - File handling and counting of occurrence of a string in files -


occurrences( inputfilenames, words, outputfilename ) 

for each file in list inputfilenames, output file called outputfilename name of input file , each word in list words, number of occurrences individual word; if of input files cannot read, issue suitable error message , skip file. added fun, without using .count() built-in function.

occurrences( ["sample1.txt","sample2.txt","sample3.txt"], ["why","you","fate","among"], "out.txt")

out.txt contains:

file name: why fate among sample1.txt 3 0 0 0 sample2.txt 2 2 1 1 sample3.txt 0 3 0 0

and i've got far

def occurrences(inputfilenames,words,outputfilename):     output = open(outputfilename,"a")      try:         file in inputfilenames:             opned = open(file,"r")             print(opned)             counters = [0 file in range (len(words))]             index = 0             in words:                 line in opned:                     if in line:                         print("word",i,"line",line)                         counters[index] += 1                 index +=1             print(counters)      except ioerror:         file.close()         print("*** occurrences: file handle error") 

i recommend using count method. example, can not see try write results output file, explain possible implementation.

def occurrences(inputfilenames, words, outputfilename):     wordcount = {}     # dictionary hold our wordcount , used construnction of output file      file in inputfilenames:         # iterate on files         try:             open(file, 'r') infile:                 content = infile.read().strip().split(" ")             # declare entry wordcount file if no ioerror raised             wordcount[file] = [0 j in range(len(words))]             in  range(len(words)):                 # instead of iterating on contents manually, split them , use count method                 wordcount[file][i] = str(content.count(words[i]))         except ioerror:             print("the file {} not read.".format(file))      open(outputfilename, 'w+') outfile:         # iterate on wordcount dict , write output         in wordcount.keys():             outfile.write(i+" "+" ".join(wordcount[i])+"\n") occurrences(["book.txt"], ["alice", "hole", "rabbit"], "occ.txt") 

occ.txt contains:

book.txt 155 0 26 

to without count method, 1 possible way iterate on content list element element , increment count if word matches element.

for in  range(len(words)):     count = 0     word in content:         if words[i] == word:             count += 1     wordcount[file][i] = str(count) 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -