# To compute the percentage increase in each crop over # the period from 2007 to 2011. # The output is written to an external file by opening # a file and writing data to it. # The output file is : foodprod1.dat # The input data file is : food.dat import numpy as np # Reads the names of crops. crops = np.loadtxt('food.dat', dtype='str', skiprows=5, usecols=(0,)) # Reads data in columns 5 to 9 fdat = np.loadtxt('food.dat', skiprows=5, usecols=(5,6,7,8,9)) f=open('foodprod1.dat','w') # This line opens a file for writing for i in range(np.size(crops)): avginc = ( fdat[i][4] - fdat[i][0] )/5.0 f.write("%14s %7.2f \n"% (crops[i], avginc)) f.close() # This line closes the file