I'm making a dictionary (in python) of a text file that is on the format:
type_of_animal / hibernation / wake_up_time / sleep_time / feeding_time
with a new type of animal on every new line. This is the code so far:
def read_animals_from_file(file_name):
'''
Makes a dictionary with the animals as keys and their attributes as values
:param file_name: Name of the file with information
:return: Dictionary with the animals and their attributes
'''
fobj = open(file_name, "r")
for line in fobj:
key , value1 , value2 , value3 , value4 = line.split("/")
animal_dict[key] = value1, int(value2), int(value3), value4
print(animal_dict)
return animal_dict
The problem is that the last value of the each line contains "\n". This is what the dictionary looks like:
{'Bear ': (' winter ', 9, 20, ' 12\n'),
'Bumblebee ': (' winter ', 6, 23, ' -\n'),
'Moose ': (' - ', 7, 19, ' 10'),
'Owl ': (' - ', 21, 5, ' 21\n'),
'Sea lion ': (' - ', 6, 18, ' 14\n'),
'Seal ': (' - ', 6, 18, ' 14\n'),
'Wolf ': (' - ', 6, 20, ' 12\n')}
I'm thinking that I should strip \n before I create the dictionary, but I can't seem to make it work. I'm also curious to how I would access a specific value, since I think they are in what's called a tuple (?). For example, how would I do if I only wanted to print the wake up time? I apologize for possible beginner mistakes :)