""" Exercise 5.16 from "A primer on..." Read files with a given srtucture and plot the results. The exercise does not require writing a function, but it is convenient to do this in order to reuse the solution for exercise 5.18. """ import sys import matplotlib.pyplot as plt import numpy as np def read_density_data(filename): temp = [] dens = [] with open(filename) as infile: for line in infile: if line[0] == '#' or line.isspace(): continue words = line.split() temp.append(float(words[0])) dens.append(float(words[1])) return np.array(temp), np.array(dens) if __name__ == "__main__": try: filename = sys.argv[1] except IndexError: print('No file given, using "density_air.txt" as default') filename = "density_air.txt" temp, dens = read_density_data(filename) plt.plot(temp, dens, 'ro') plt.show() """ Terminal> python read_density_data.py No file given, using "density_air.txt" as default (output is a plot) """