This function is meant to read a file into a dictionary, using the birdnames a keys and the weights as values. It is doing what I want but it isnt going through all the lines and im not sure why! Help a girl out? Here is my code:
def bird_weights(filename):
bird_dict = {}
f = open(filename, "r")
for line in f:
new_line = line.split(":")
bird_type = new_line[0].capitalize()
bird_weight = new_line[1].strip().split(' ')
bw_list = [float(i) for i in bird_weight]
bird_dict[bird_type] = bw_list
if bird_type in bird_dict:
bird_dict[bird_type].extend(bw_list)
else:
bird_dict[bird_type] = bw_list
return bird_dict
the .txt file is:
bluebird:78.3 89.3 77.0
TANAGER: 111.9 107.65
BlueBird: 69.9
bluebirD: 91.9
tanager: 108.0 110.0
and the code is meant to produce
{"Bluebird":[78.3, 89.3, 77.0, 69.9, 91.9],"Tanager": [111.9, 107.65, 108.0, 110.0]}
what i am getting is:
{"Bluebird":[91.9, 91.9], "Tanager": [108.0, 110.0, 108.0, 110.0] }
I am not sure why