""" Ex. 5.16 from A primer on... Two main tasks: 1. Read data from file, store in lists or arrays 2. Plot data We create a function and a module to facilitate code reuse in Ex 5.18. """ import sys import matplotlib.pyplot as plt def read_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 temp, dens if __name__ == "__main__": """The if-test above ensures that this code is run if the file is used as a stand-alone program, but not if it is imported into another program as a module. """ filename = sys.argv[1] t, d = read_data(filename) plt.plot(t,d,'ro') plt.show() """ Terminal> python read_density_data.py density_air.txt (output is a plot) Terminal> python read_density_data.py density_water.txt (output is a plot) """