0

Print the top 5 most frequent words in a text file

Good evening, I am in the process of creating a script to search and print specific information contained in a text file with over 400K lines. I have narrowed down the lines to only include lines that contain the word "TCP" which total to 40K. Within the if statement (also in a loop) that narrowed down the lines, I need to then print the top 5 words: for line in file1: if line != ' ': # Will search for lines that contain TCP. if "PROTO=TCP" in line: x = x + 1 For example, each line in the file contains the string DST=(a particular number). I was wondering how can I go through 40k lines to only look for DST=# and present them as the top 5 most frequent? Is there a resource I can use to figure this out? Any help with this would be much appreciated.

1st Sep 2019, 9:28 PM
Progress101
Progress101 - avatar
1 Antwort
0
look up counter from the collection module in python.org. there's an example there that does exactly what you're looking to do.
1st Sep 2019, 9:39 PM
rodwynnejones
rodwynnejones - avatar