Here is an example line from your file:
/a/abbey/sun_aqswjsnjlrfzzhiz.jpg
This gets read into the line
variable by line = f.readline()
so the while loop doesn't exit right away because of the empty line variable.
Then in the while loop line = line[3:-26]
crops down the first 3 characters and the last 26 characters (because it is a line of a file, you need to count the return character at the end of each line, hence 26 instead of 25) so for this line your line variable will hold this: abbey
.
The if statement in the while loop:
if line in counter_dict:
counter_dict[line] += 1
else:
counter_dict[line] = 1
If the value of the line
variable which holds abbey
at this point is found in the counter_dict
than increments its value by 1 in the dictionary. If it is not found than adds it to the dictionary with value set to 1.
The last thing the while loop does is to get the next line from the file with line = f.readline()
.
It will keep doing that until there is no next line and than it prints the resulting json file.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…