0
from collections import defaultdict
d = {}
with open("sample.log") as samp:
    for line in samp:
        if line.split():
            parts = line.split()
            # 0 because if it's not found, the count should be 0
            d[parts[2]] = d.get(parts[2], 0) + 1

    print(d)
    dd = defaultdict(int)
    for line in samp:
        parts = line.strip().split()
        if line.split():
            dd[parts[2]] += 1
    print(dd)

the link for the l: https://drive.google.com/file/d/1o1ZSvJDA7n6diVNeJztQMMecBUgQUQu9/view?usp=sharing

Rebecca Bibye
  • 190
  • 2
  • 18
  • 1
    I can't understand the question. What "doesn't work"? What happens when you run the code, and how is that different from what is supposed to happen? – Karl Knechtel Dec 07 '20 at 18:42
  • 1
    Oh, I see what you mean. The second one, whichever it is, will not receive any `line`s from `samp`. This is because of https://stackoverflow.com/questions/3906137/why-cant-i-call-read-twice-on-an-open-file and has nothing to do with `defaultdict`. – Karl Knechtel Dec 07 '20 at 18:44
  • 2
    The first `for line in samp` loop reads all the lines, and there aren't any lines left to read for the second loop. If you want to loop over the file more than once, use `lines = samp.readlines()` and you can loop over `lines` as many times as you want. – John Gordon Dec 07 '20 at 19:14

0 Answers0