Using python to get the number of keywords in txt file

  • 2021-08-31 08:42:45
  • OfStack

Origin:

Developers need the access request amount of one project in tomcat in one month. For other reasons, only the method of finding tomcat request log is left. Just recently, they are learning python, so they explored it with python;

General idea:

1. Copy the corresponding tomcat log file to the machine with python environment

2. Use os. listdir () to get the list of all file names in the directory, and then use for to loop through the list and string splicing to get the specific path of the file name

3. Read the file with open (). In the following code, for line in f: Read the contents of txt file by line (1 line 1 read will not load all the file contents)

4. Use count () method to count keywords (strings) named after projects

Knowledge points:

File reading and count () method

The Python count () method counts the number of occurrences of a character in a string. Optional parameters are the start and end positions of string search.

count () method syntax: str. count (sub, start=0, end=len (string))

Parameters:

sub--Substring of search start--Where the string starts searching. The default is the first character, and the index value of the first character is 0. end--The position in the string where the search ends. The index of the first character in the character is 0. The default is the last 1 position of the string.

The code is as follows

(If there are too many files, you can add a work queue (gevent library)):


import os

#  Type the documents and count them 
def read_log(url,keyword):
  count = 0
  with open(url,'r',encoding='utf-8') as f:  #  Open a file 
  for line in f:                  #  Read by line txt Documents 
    count += line.count(keyword,53,64)  # count() Method count ,keyword Is the passed-in keyword ( String )
return count

path= 'E:\\python\\vscode\\ Work \\log\\80\\'
dirlist = os.listdir(path)  #  Get path All under the path txt Filename 

sum = 0
for name in dirlist:         #  Traversal acquisition txt Filename 
  url = path + name
  num = read_log(url,keyword)
  print(str(name) + '  The number in the file is : ' + str(num))
  sum += num
print(' Total number of keywords:  ' + str(sum))

The above is the python access to txt file keyword number details, more information about python access to keywords please pay attention to other related articles on this site!


Related articles: