Run the template code. Study the contents of the code and the output file.
First, we create an empty dictionary. We will use this dictionary in the following way: each key will be a unique log level from the input file, and its corresponding value will be an integer indicating how many logs were found in the input file at that given level.
Next, we open the input file using a DictReader
(you can use csv.reader
if you prefer) and read it line by line. For each line, we check whether the value in the log_level
field is already present as a key in the log_levels
dictionary. If it is, then we increment the key's value by 1. If it is not, this means we've found a new unique logging level. In that case, we add the logging level as a new key in the dictionary and assign an initial value of 1 to it.