Last active
February 14, 2019 00:17
-
-
Save AO8/7dec809f0300df8b0923f2771ebb7029 to your computer and use it in GitHub Desktop.
A simple Python 3 app to explore your Amazon.com purchase history CSV data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import sys | |
from collections import Counter, defaultdict | |
from datetime import datetime as dt | |
from decimal import Decimal | |
from time import sleep | |
# First import your purchases data from Amazon.com and store CSV | |
# file in the same directory as this program. | |
# Column names in CSV file for reference | |
# based on customer order download from Amazon.com as of 2/11/19. | |
# You can also print column names on CSV with print_csv_column_names(). | |
# [1] Order Date [13] Shipping Address Zip | |
# [2] Order ID [14] Order Status | |
# [3] Payment Instrument Type [15] Carrier Name & Tracking Number | |
# [4] Website [16] Subtotal | |
# [5] Purchase Order Number [17] Shipping Charge | |
# [6] Ordering Customer Email [18] Tax Before Promotions | |
# [7] Shipment Date [19] Total Promotions | |
# [8] Shipping Address Name [20] Tax Charged | |
# [9] Shipping Address Street 1 [21] Total Charged | |
# [10] Shipping Address Street 2 [22] Buyer Name | |
# [11] Shipping Address City [23] Group Name | |
# [12] Shipping Address State | |
def main(): | |
print_header() | |
user_csv_file = input("\tEnter the name of your Amazon CSV file,\n"\ | |
"\tthen press <enter>: ").strip() | |
try: | |
with open(user_csv_file, "r") as f: | |
print() | |
sleep(1) # impression of program crunching data, that's all | |
print("\tSuccess!") | |
sleep(1) | |
print() | |
run_event_loop(user_csv_file) | |
except (FileNotFoundError, Exception) as e: | |
print(f"\n\tUh, oh. There was a problem opening this CSV file:\n\n\t{e}", | |
file=sys.stderr) | |
print("\n\tPlease try again or enter a different CSV filename.\n", | |
file=sys.stderr) | |
main() | |
def run_event_loop(user_csv_file): | |
"""event loop asks user what they would like to do until quitting""" | |
get_date_range_from_csv(user_csv_file) | |
sleep(1) | |
while True: | |
print("\n\tChoose from the following options:\n") | |
print("\t[1] Get total number of purchases") | |
print("\t[2] Get total amount spent") | |
print("\t[3] Get number of purchases per year") | |
print("\t[4] Get purchases per month for a given year") | |
print("\t[5] Get summary of package carriers") | |
print("\t[6] Exit app\n") | |
user_choice = input("\tWhat would you like to do? ").strip() | |
print() | |
# [1] Get total number of purchases | |
if user_choice == "1": | |
purchases = get_total_number_of_purchases(user_csv_file) | |
print(f"\tHere are your total purchases on Amazon: {purchases}\n") | |
print("-".center(79, "-")) | |
sleep(2) | |
# [2] Get total amount spent | |
elif user_choice == "2": | |
amount_spent = get_total_amount_spent(user_csv_file) | |
print(f"\tYou spent a grand total of ${amount_spent}.\n") | |
print("-".center(79, "-")) | |
sleep(2) | |
# [3] Get number of purchases per year | |
elif user_choice == "3": | |
purchases = get_purchases_per_year(user_csv_file) | |
print("\tYou purchases per year are as follows:\n") | |
for year, count in purchases.items(): | |
print(f"\t{year} = {count}") | |
print() | |
print("-".center(79, "-")) | |
sleep(2) | |
# [4] Get purchases per month for a given year | |
elif user_choice == "4": | |
user_year = int(input("\tWhat year would you like to query? ")) | |
purchases = get_purchases_per_month(user_csv_file, user_year) | |
print(f"\n\tHere are your monthly number of purchases in {user_year}:\n") | |
for month, count in purchases: | |
print(f"\t{month:<10} {count}") | |
print() | |
print("-".center(79, "-")) | |
sleep(2) | |
# [5] Get summary of package carriers | |
elif user_choice == "5": | |
carriers = get_summary_of_carriers(user_csv_file) | |
print("\tHere is your total package carrier summary:\n") | |
for year, count in carriers.most_common(): | |
print(f"\t{year:<18} {count}") | |
print() | |
print("-".center(79, "-")) | |
sleep(2) | |
# Exit event loop | |
elif user_choice == "6": | |
print("\tThanks! Have a nice day.") | |
break | |
# Check for invalid input from user | |
else: | |
print("\tInvalid selection! Please choose from the following:\n", | |
file=sys.stderr) | |
sleep(2) | |
def print_header(): | |
print("".center(79, "-")) | |
print() | |
print(" AMAZON PURCHASE HISTORY ANALYZER ".center(79)) | |
print() | |
print("".center(79, "-")) | |
print() | |
def get_date_range_from_csv(filename): | |
"""print purchase history range for user's csv file""" | |
with open(filename, "r") as f: | |
reader = csv.DictReader(f) | |
order_dates = [row["Order Date"] for row in reader] | |
start = order_dates[0] | |
end = order_dates[-1] | |
print(f"\tYour CSV file contains purchase history data for:\n") | |
print(f"\t{start} through {end}") | |
def get_total_number_of_purchases(filename): | |
"""returns a count of total purchases from csv file""" | |
with open(filename, "r") as f: | |
reader = csv.reader(f) | |
row_count = sum(1 for row in reader) - 1 # minus headings | |
return row_count | |
def print_csv_column_names(filename): | |
"""function for user to print column names in csv file if needed""" | |
with open(filename, "r") as f: | |
reader = csv.DictReader(f) | |
column_names = reader.fieldnames | |
for idx, column in enumerate(column_names, 1): | |
print(f"{[idx]}: {column}") | |
def get_total_amount_spent(filename): | |
"""returns total amount spent in csv file""" | |
total = 0 | |
with open(filename, "r") as f: | |
reader = csv.DictReader(f) | |
for row in reader: | |
# strip $ sign and occasional right whitespace | |
amount = Decimal(row["Total Charged"][1:].rstrip()) | |
total += amount | |
return "{:,}".format(total) | |
def get_purchases_per_year(filename): | |
"""asks user to enter a year and returns total number of purchases for that year""" | |
buys_per_year = Counter() | |
with open(filename, "r") as f: | |
reader = csv.DictReader(f) | |
for row in reader: | |
year = row["Order Date"][-4:] | |
buys_per_year[year] += 1 | |
return buys_per_year | |
def get_summary_of_carriers(filename): | |
"""returns a dict counter of carriers who have delivered packages. | |
Counter's conveninent most_common() method is helpful for displaying | |
sorted data, fyi.""" | |
carrier_names = Counter() | |
with open(filename, "r") as f: | |
reader = csv.DictReader(f) | |
for row in reader: | |
if row["Carrier Name & Tracking Number"]: | |
items = row["Carrier Name & Tracking Number"].split("(") | |
carrier = items[0] | |
carrier_names[carrier] += 1 | |
return carrier_names | |
def get_purchases_per_month(filename, year): | |
"""gets buys per month for a given year as a tuple of (month, total buys)""" | |
buys_per_month = defaultdict(int) | |
with open(filename, "r") as f: | |
reader = csv.DictReader(f) | |
for row in reader: | |
date = row["Order Date"].split("/") # ['4', '10', '2010'] | |
date_obj = dt(year=int(date[2]), | |
month=int(date[0]), | |
day=int(date[1])) | |
month = date_obj.strftime("%B") | |
if date_obj.year == year: | |
buys_per_month[month] += 1 | |
months_list = ["January", "February", "March", "April", | |
"May", "June", "July", "August", "September", | |
"October", "November", "December"] | |
sorted_monthly_buys = [(key, buys_per_month[key]) | |
for key in months_list] | |
return sorted_monthly_buys | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment