1

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']

  1. How do I split the list so that everything is its own element?
  2. 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?
Karluk
  • 17
  • 1
  • 6
  • why don't you try using Dictionary? – Muntasir Aonik Nov 11 '22 at 10:48
  • @MuntasirAonik How do I make a dictionary from the txt file? – Karluk Nov 11 '22 at 10:50
  • What is the exact output you want here? – Tim Biegeleisen Nov 11 '22 at 10:51
  • @Karluk add your code to the question. – Muntasir Aonik Nov 11 '22 at 10:52
  • 1
    [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Yevhen Kuzmovych Nov 11 '22 at 10:55
  • @TimBiegeleisen I want to use .readlines() to make either a list or dictionary for the elements on each row. Then I would like to for example if a user input is 14 to print out Bear because it has waking hours between 9-20. Basically I want the name to be connected to all the attributes – Karluk Nov 11 '22 at 10:58
  • @YevhenKuzmovych Noted! – Karluk Nov 11 '22 at 11:00
  • @Karluk You need to specify exactly how you would like the output to look like and/or describe how you would use the data, otherwise people won't be able to answer your question without guessing or assuming what you want, which is not ideal. Please edit your question and add the code you have as text and not pictures, then describe what you expect the output to look like. – Cow Nov 11 '22 at 11:04
  • Either have a list of dictionaries (keyed on animal type) or, better, write an Animal class. Then you could have a list of Animals (classes) – DarkKnight Nov 11 '22 at 11:48
  • @Cobra Animal class sounds like the way to go. How would I go about adding each animal as a separate instance? Should I use a while loop and then use the index to add each animals separately? – Karluk Nov 11 '22 at 11:53
  • @user56700 Better? – Karluk Nov 11 '22 at 11:53

1 Answers1

2

The file format is essentially CSV (albeit that the separators are not commas). You could use pandas or the csv module but as this so trivial I suggest:

class Animal:
    def __init__(self, animal, hibernation, waking_hours, feeding_time):
        self._animal = animal
        self._hibernation = hibernation
        self._waking_hours = waking_hours
        self._feeding_time = feeding_time
    def __str__(self):
        return f'Animal={self._animal}, Hibernation={self._hibernation}, Waking hours={self._waking_hours}, Feeding time={self._feeding_time}'

list_of_animals = []

with open('Animals.txt') as afile:
    for line in afile.readlines()[1:]:
        list_of_animals.append(Animal(*map(str.strip, line.split('/'))))

print(*list_of_animals, sep='\n')

Output:

Animal=Bear, Hibernation=winter, Waking hours=9-20, Feeding time=12
Animal=Nightowl, Hibernation=-, Waking hours=21-05, Feeding time=21
Animal=Sealion, Hibernation=-, Waking hours=6-18, Feeding time=14
Animal=Seal, Hibernation=-, Waking hours=6-18, Feeding time=14
Animal=Wolf, Hibernation=-, Waking hours=6-20, Feeding time=12
Animal=Moose, Hibernation=-, Waking hours=7-19, Feeding time=10
DarkKnight
  • 19,739
  • 3
  • 6
  • 22