""" Ex. 6.9 from 'A primer on...', but implemented using a class instead of dictionaries. We define a class which holds the necessary information for one human species (one line in the file), and the total information is stored as a list of instances of this class. The class contains a special method __str__ which writes the information in the same format as the original file. """ class Human: def __init__(self, name, when, height, mass, brain): self.name = name self.when = when self.height = height self.mass = mass self.brain = brain def __str__(self): species = self.name w = self.when h = self.height m = self.mass b = self.brain line = f'{species:20s} {w:14s} {h:10s} {m:9s} {b}' return line humans = [] with open('human_evolution.txt') as infile: for line in infile: if line[0] == 'H': species = line[:21].strip() when = line[21:37].strip() height = line[37:50].strip() mass = line[50:62].strip() brain = line[62:].strip() H = Human(species, when, height, mass, brain) humans.append(H) for h in humans: print(h) #this only works since we have defined the special method __str__ in the class """ Terminal> python humans_class.py H. habilis 2.2 - 1.6 1.0 - 1.5 33 - 55 660 H. erectus 1.4 - 0.2 1.8 60 850 (early) - 1100 (late) H. ergaster 1.9 - 1.4 1.9 700 - 850 H. heidelbergensis 0.6 - 0.35 1.8 60 1100 - 1400 H. neanderthalensis 0.35 - 0.03 1.6 55 - 70 1200 - 1700 H. sapiens sapiens 0.2 - present 1.4 - 1.9 50 - 100 1000 - 1850 H. floresiensis 0.10 - 0.012 1.0 25 400 """