""" 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 = [] density = [] with open(filename) as infile: for line in infile: if line[0] == '#' or line.isspace(): continue #skip the rest and move to next loop iteration words = line.split() temp.append(float(words[0])) density.append(float(words[1])) return temp, density """ The lines below will be run if we run the code as a standalone program, i.e. python read_density_data.py but not if we import it as a module into another program: """ if __name__ == '__main__': file = sys.argv[1] temp, dens = read_data(file) plt.plot(temp,dens,'ro') plt.show() """ Terimal> python read_density_data.py density_water.dat Output is a plot """