# # program opens a hard data file with that contains on each line # # name(string) hours_worked(int) pay_per_hour(float) # # and generates a table the information read, plus the total pay # for the person. # # sample line from input file: # # Jennifer 25 11.23 # # written by e.bonakdarian dec 2009 # updated nov 2010 # ############################################# def readFile(filename): # reads a file and returns a list of lines inf = open(filename, 'r') data = inf.readlines() inf.close() return data ############################################# def processOutput(text, out_filename): # open and process the file, also write it # output file (really, this function should be # split up into two probably) # outf = open(out_filename, 'w') # format the output and disply both to screen and write to file # print table header print '%-8s %10s %8s %10s' % ('Name', 'Hours', 'Pay', 'Total') print '-----------------------------------------------' # loop through the data for line in text: part = line.split() name = part[0] hours = int(part[1]) pay_rate = float(part[2]) # output to screen print('%-11s %5d $%7.2f $%8.2f' % (name, hours, pay_rate, hours * pay_rate)) # output to file print >> outf, ('%-11s %5d $%7.2f $%8.2f' % (name, hours, pay_rate, hours * pay_rate)) outf.close() ############################################# def main(): input_filename = 'pay_data.txt' text = readFile(input_filename) lines = len(text) print 'read %d lines of text from file "%s"\n' % (lines, input_filename) output_filename = 'payout.txt' processOutput(text, output_filename) print '\nOutput also written to file "%s"' % output_filename ############################################# if __name__ == '__main__': main() raw_input('\n to exit')