Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
keyword matching part2
#13
(Dec-18-2018, 02:01 PM)buran Wrote: What have you tried? We are glad to help, but we are not going to do it for you.

tried below but it's giving wrong line no.
#importing required packages

import glob
from collections import Counter
import re
import xlwt
from xlwt import Workbook
import xlsxwriter 
import xlrd 
import errno
import time
from datetime import datetime
import datetime
import os
import os.path
import warnings
from xlutils.copy import copy
import openpyxl
 
# opening excel file
 
from xlrd import open_workbook
 
warnings.filterwarnings("ignore")
timestr = time.strftime("%Y%m%d-%H%M%S")
 
# path where all the folders and sub folders need to be searched.
 
yourpath = "D:\\mainfolder\\subfolders"
 
 
# location where excel containing all the keywords which are to be searched in above path.
loc = ("D:\\sample.xlsx")
 
cnt = Counter()
wb = xlrd.open_workbook(loc) 
sheet = wb.sheet_by_index(0)
rows = sheet.nrows
 
excel_word=[]
# loop to pick up all the keywords from the column of excel one after another.
 
for i in range(1,rows):
    excel_word.append(sheet.cell_value(i,1))
 
# report to be generated in this location.
report_txt="D:\\mainfolder\\report"+timestr+".txt"
 
# report is opened in write mode.
 
FO = open(report_txt, 'w')
 
# structure layout of text file where records will be written.
 
str3="|"+"Pattern"+" "*(20-len("Pattern"))+"|"+"Vuernabilitiy  in file"+" "*(200-len("Vuernabilitiy  in file"))+"|"+"Line No"+" "*(10-len("Line No"))+"|" +"\n"
FO.write(str3)
FO.write("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"+"\n")
 
# main logic 
 
for root, dirs, files in os.walk(yourpath, topdown=False):
     
    line=0
    for name in files:
         
        path=os.path.join(root, name)
        files = glob.glob(path)
         
        for name in files:
                 
                 
                try:
                    with open(name,encoding="utf8",errors='ignore') as f:
                         
                        text_string1 = f.read()
                        for i in range(0,len(excel_word)):
                            str2=''
                            for num, line in enumerate(name, 1):
                                if excel_word[i] in text_string1:
                                    cnt[excel_word[i]] += 1
                             
                                    str2="|"+excel_word[i]+" "*(20-len(excel_word[i]))+"|"+os.path.join(root, name)+" "*(200-len(os.path.join(root, name)))+"|"+str(num)+" "*(10-len(str(num)))+"|" +"\n"
                                 
                            else:
                                cnt[excel_word[i]]+=0
                            FO.write(str2)
                             
                             
                                           
                except IOError as exc:
                    if exc.errno != errno.EISDIR:
                        raise
 
FO.close()

Any updates by experts please?

Thanks
Reply


Messages In This Thread
Keyword matching - by janho - Dec-18-2018, 01:50 PM
RE: keyword matching part2 - by micseydel - Jan-02-2019, 11:04 PM
RE: Keyword matching - by buran - Dec-18-2018, 02:01 PM
RE: Keyword matching - by janho - Dec-21-2018, 04:02 AM
RE: Keyword matching - by micseydel - Dec-21-2018, 07:43 PM
RE: Keyword matching - by janho - Dec-25-2018, 10:38 AM
RE: Keyword matching - by janho - Dec-26-2018, 11:21 AM
RE: Keyword matching - by janho - Dec-27-2018, 11:00 AM
RE: Keyword matching - by janho - Dec-28-2018, 02:35 AM
RE: Keyword matching - by woooee - Dec-28-2018, 02:56 AM
RE: Keyword matching - by janho - Dec-28-2018, 01:07 PM
RE: Keyword matching - by janho - Dec-28-2018, 03:11 PM
RE: Keyword matching - by janho - Dec-31-2018, 04:27 AM
RE: Keyword matching - by janho - Dec-31-2018, 09:01 AM
RE: Keyword matching - by micseydel - Dec-31-2018, 05:19 PM
RE: Keyword matching - by janho - Jan-01-2019, 04:58 AM
RE: Keyword matching - by janho - Jan-01-2019, 06:55 AM
keyword matching part2 - by janho - Jan-01-2019, 12:35 PM
RE: Keyword matching - by janho - Jan-01-2019, 12:41 PM
RE: Keyword matching - by janho - Jan-02-2019, 05:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Find a specific keyword after another keyword and change the output sgtmcc 5 924 Oct-05-2023, 07:41 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020