-2

I have a file.txt with about 1,000 lines that look like this:

--- Adding sections to FwLogger: [],2020-01-13 16:09:18,2020-01-13 16:09:22 

--- Clearing all sections from FwLogger,2020-01-13 16:09:17,2020-01-13 16:09:22   
--- (1/0) The value was discarded due to being too separated from previous value    
--- (1/0) ContinueBoot@b7630fd  Rebooting device due to capabilities request freeze

And I would need to know how many times the word "FwLogger" appears ( in number ).

wibeasley
  • 5,000
  • 3
  • 34
  • 62
  • Anything you have tried so far? – FlyingTeller Jan 15 '20 at 14:59
  • Welcome to Stack Overflow. Read and include features from [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822). – wibeasley Jan 15 '20 at 15:01
  • Does this answer your question? [How to find the count of a word in a string?](https://stackoverflow.com/questions/11300383/how-to-find-the-count-of-a-word-in-a-string) – Maurice Meyer Jan 15 '20 at 15:35

2 Answers2

0

There are definitely more elegant ways to do it, but in my version you replace the delimiters manually:

with open('test.txt') as file:
    for line in (line.strip() for line in file):
       #here you replace all possible delimiters in your file with a space to split afterwards according to the spaces
        c=line.replace(","," ").replace(";"," ").replace("@"," ").replace(":"," ")
        for word in c.split(" "):
            if word == "FwLogger":
#                print(line)
                counter= counter+1
                print(counter)
Mattis Seehaus
  • 114
  • 1
  • 11
-1

read in your txt file and use the string find method like below

loop

istart = str.find(sub, istart)

I= I + 1.

end loop

I start is the position where the string you're looking for was last found. before starting your loop assign istart = 1

each time one is found increment a counter

i.e. I= I + 1

M.Cairns
  • 39
  • 11