This is my function:
def read_file():
textfile = open("Animals.txt", "r", encoding="utf-8")
list = textfile.readlines()
print(list)
textfile.close()
This is Animals.txt:
Animal / Hibernation / Waking Hours / Feeding Time
Bear / winter / 9-20 / 12
Nightowl / - / 21-05 / 21
Sealion / - / 6-18 / 14
How do make a list where the information on each line relates to the animal Name. For example I would like Bear to be connected with winter, 9-20, 12 and also be separated from Nightowl.
My first idea was to separate the list with .split and then moving forward using the list index to navigate the elements. Just printing the list gave this:
['Bear / winter / 9-20 / 12\n', 'Nightowl / - / 21-05 / 21\n', 'Sealion / - / 6-18 / 14 \n', 'Seal / - / 6-18 / 14\n', 'Wolf / - / 6-20 / 12\n', 'Moose / - / 7-19 / 10\n']
- How do I split the list so that everything is its own element?
- Is it better to use a dictionary and if so how do I make a dictionary where the Name i.e Bear correlates to all the other elements?