[Solved] I need help to understand logic and solve code using python


In your code you are opening the file in with statement, that means it closes automatically. No need to call close() explicitly.

For any string manipulation/searching it’s good to know Regular expression (in Python re module). For counting variables you could use Counter from itertools. Here’s an example:

from collections import Counter
import re

with open('logfile.txt', 'r') as f:
    data_in = f.read()

columns = re.findall(r'([\d\/]+)\s+([\d\.]+)\s+([\d:]+)\s+([a-zA-Z]+)', data_in)
c = Counter(columns)

for (column_1, column_2, column_3, column_4), count in c.items():
    print(column_1, column_2, column_3, column_4, count)

Prints:

9/1/2013 1.1.1.1 0:10 Established 2
9/1/2013 1.1.1.2 0:10 Failed 1
9/1/2013 1.1.1.1 0:10 Syn 1
9/2/2013 1.1.1.1 0:10 Established 4
9/2/2013 1.1.1.1 0:10 Failed 3
9/2/2013 1.1.1.2 0:10 Established 1
9/2/2013 1.1.1.1 0:10 Syn 2

solved I need help to understand logic and solve code using python