Python 2.7.3: Search/Count txt file for string, return full line with final occurrence of that string -
i'm trying create wifi log scanner. go through logs manually using ctrl+f , our keywords. want automate process. i.e. bang in .txt file , receive output.
i've got bones of code, can work on making pretty later, i'm running little issue. want scanner search file (done), count instances of string (done) , output number of occurrences (done) followed total line string occurred last, including line number (line number not essential, makes things easier gestimate of more recent issue if there multiple).
currently i'm getting output of every line string in it. know why happening, can't think of way specify output lastly line.
here code:
import os tkinter import tk tkfiledialog import askopenfilename def file_len(filename): #count number of lines in file , output result open(filename) f: i, l in enumerate(f): pass print('there ' + str(i+1) + ' lines in ' + os.path.basename(filename)) def file_scan(filename): #all issues scan go here print ("dhcp found " + str(filename.count('no lease, failing')) + " time(s).") line in filename: if 'no lease, failing' in line: print line.strip() dns= (filename.count('host name lookup failure:res_nquery failed') + filename.count('http query failed'))/2 print ("dns failure found " + str(dns) + " time(s).") line in filename: if 'host name lookup failure:res_nquery failed' or 'http query failed' in line: print line.strip() print ("psk= found " + str(testr.count('psk=')) + " time(s).") line in ln: if 'psk=' in line: print 'the length(s) of psk used ' + str(line.count('*')) tk().withdraw() filename=askopenfilename() abspath = os.path.abspath(filename) #so doesn't matter if file in python dir dname = os.path.dirname(abspath) #so doesn't matter if file in python dir os.chdir(dname) #so doesn't matter if file in python dir print ('report ' + os.path.basename(filename)) file_len(filename) file_scan(filename)
that's, pretty much, going working code (just have add together few more issue searches), have version searches string instead of text file here. outputs following:
total number of lines: 38 dhcp found 2 time(s). dhcp dhcp psk= found 2 time(s). length(s) of psk used 14 length(s) of psk used 8
i have general stuff there, modified beingness string rather txt file, string i'm scanning what's in txt files.
don't worry much psk, want examples of listed, i'll see if can tidy them 1 line @ later stage.
as side note, lot of jumbled doing previous searches, have thought there neater ways of doing this. not current concern, if have suggestion on side of things, please provide explanation/link explanation why way better. i'm new python, i'm dealing stuff understand. :)
thanks in advance help, if need farther info, please allow me know.
joe
to search , count string occurrence solved in next way
'''---------------------function--------------------''' #counting "string" occurrence in file def count_string_occurrence(): string = "test" f = open("result_file.txt") contents = f.read() f.close() print "number of '" + string + "' in file", contents.count("foo") #we searching "foo" string in file "result_file.txt"
python-2.7 full-text-search
No comments:
Post a Comment