"""Ex. 6.9 from 'A primer on... Read info from a file into a nested dictionary. File is structured in a way that makes it difficult to process each line with the split-function. We can instead use string indexing. Finally, print the info from the diction """ #read file and store in a nested dict: humans = {} with open('human_evolution.txt') as infile: for line in infile: if line[0] == 'H': species = line[:20].strip() when = line[21:37].strip() height = line[37:50].strip() mass = line[50:62].strip() brain = line[62:].strip() info = {'when': when, 'height': height, 'mass': mass, 'brain volume': brain} humans[species] = info """Print info from dict to the screen, in nicely formatted columns. Notice the format specifiers to ensure correct alignment of the columns.""" for key, value in humans.items(): species = key w = value['when'] h = value['height'] m = value['mass'] b = value['brain volume'] line = f'{key:20s} {w:14s} {h:10s} {m:9s} {b}' print(line) """ Terminal> python humans.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 """