Can you explain what is going on in this code? I do not seem to understand how you can open a file and read it in turn instead of all the sentences at the same time in a for loop. Thanks
Let's say I have these sentences in a document file:
cat:dog:mice cat1:dog1:mice1 cat2:dog2:mice2 cat3:dog3:mice3
Here is the code:
from sys import argv filename = input("Please enter the name of a file: ") f = open(filename,'r') d1ct = dict() print("Number of times each animal visited each station:") print("Animal Id Station 1 Station 2") for line in f: if '\n' == line[-1]: line = line[:-1] (AnimalId, Timestamp, StationId,) = line.split(':') key = (AnimalId,StationId,) if key not in d1ct: d1ct[key] = 0 d1ct[key] += 1
source share