相对于导出EXCEL文件,PYTHON计算更为实时。
import csv import sys from optparse import OptionParser def calculate_profit(day): return float(day['Revenue']) - float(day['Cost']) if __name__ == '__main__': parser = OptionParser() parser.add_option('-f', '--file', help="CSV Data File") opts, args = parser.parse_args() if not opts.file: parser.error('File name not exist.') #Create a dict reader from an open file #handle and iterate through rows. reader = csv.DictReader(open(opts.file, 'rU')) for day in reader: print('%10s: %10.2f' % (day['Date'], calculate_profit(day)))
原始CSV文件:
Date,Revenue,Cost 3-May-10,1289.41,889.56 4-May-10,951.89,753.23 5-May-10,2812.23,2541.23 6-May-10,554.25,321.56 7-May-10,1985.65,1536.86 8-May-10,453.36,369.25 9-May-10,2236.87,2003.68 10-May-10,1825.63,1658.96 11-May-10,951.89,852.36