Skip to content

Instantly share code, notes, and snippets.

@yellowcrescent
Last active June 20, 2018 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yellowcrescent/cc13e761e775f4aa5f0d091f9103a6df to your computer and use it in GitHub Desktop.
Save yellowcrescent/cc13e761e775f4aa5f0d091f9103a6df to your computer and use it in GitHub Desktop.
Calculate total Amazon spending from Order Report
#!/usr/bin/env python
# vim: set ts=4 sw=4 expandtab:
"""
amazon_total.py
Calculate total cost of Amazon orders
Usage:
- Login to your Amazon account, then click on 'Download order reports'
Choose the time period, and Amazon will generate a CSV file for you.
Make sure to choose the 'Orders and Shipments' report
- Run this script against the CSV file you just saved:
./amazon_total.py my_2017_amazon_report.csv
@author J. Hipps <jacob@ycnrg.org>
"""
import sys
import csv
def get_total_from_csv(csvpath):
try:
with open(csvpath, 'r') as f:
odata = [x for x in csv.DictReader(f)]
except Exception as e:
print("ERROR: Failed to parse CSV file: %s" % (str(e)))
sys.exit(100)
gtot = sum([float(x['Total Charged'][1:]) for x in odata])
stot = sum([float(x['Subtotal'][1:]) for x in odata])
ttot = sum([float(x['Tax Charged'][1:]) for x in odata])
shtot = sum([float(x['Shipping Charge'][1:]) for x in odata])
print("Orders : {}".format(len(odata)))
print("-----------------------------------------------")
print("Subtotal : ${:>10.2f}".format(stot))
print("Tax : ${:>10.2f}".format(ttot))
print("Shipping : ${:>10.2f}".format(shtot))
print("-----------------------------------------------")
print("TOTAL : ${:>10.2f}\n".format(gtot))
def _main():
if len(sys.argv) < 2:
print("Usage: amazon_total.py REPORT.csv")
sys.exit(1)
get_total_from_csv(sys.argv[1])
if __name__ == '__main__':
_main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment