Skip to content

Instantly share code, notes, and snippets.

@2hands10fingers
Created November 18, 2018 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 2hands10fingers/d5e141de44ac99abce40cbf425dddb27 to your computer and use it in GitHub Desktop.
Save 2hands10fingers/d5e141de44ac99abce40cbf425dddb27 to your computer and use it in GitHub Desktop.
import csv
kitchen =[]
pastry = []
bakery = []
elseer = []
time = "morn"
def time_loc(loc):
return loc + "-" + time
locations = ['FortWorth', 'Grapevine', 'OakLawn', 'LoversLane', 'Plano','PrestonRoyal']
for location in map(time_loc, locations):
the_locale = 'pre' + location + '.csv'
print("Opening", the_locale)
with open(the_locale, 'r') as file:
csv_reader = csv.reader(file)
n = 1
next(csv_reader)
for i in csv_reader:
n += 1
x = {
"quantity" : int(i[41]),
"item_name": i[39].replace('u201d', '').replace('\\', ""),
"category": str(i[40])
}
category = x["category"]
if category.startswith('kitchen'):
kitchen.append(x)
elif category.startswith('pastry'):
pastry.append(x)
elif category.startswith('bakery'):
bakery.append(x)
else:
elseer.append(x)
if len(elseer) > 0:
SystemExit(f'{the_locale}: {elseer}')
def items_list(the_list):
items = {}
for i in the_list:
item_name = i["item_name"]
quantity = i["quantity"]
item_key = items.get(item_name, item_name)
if item_key == item_name:
items[item_name] = quantity
if item_key != item_name:
items[item_name] = items[item_name] + quantity
return items
categories = [items_list(kitchen), items_list(bakery), items_list(pastry)]
with open(location + '.csv', 'w') as write_file:
writer = csv.writer(write_file)
category = ''
count = 0
def column_names(category):
if category == 'Kitchen':
writer.writerow([category, "Item Name", "Quantity"])
else:
writer.writerow([category, "", ""])
for i in categories:
if count == 0:
column_names('Kitchen')
if count == 1:
column_names('Bakery')
if count == 2:
column_names('Pastry')
count += 1
for k,v in i.items():
writer.writerow(["", k, v])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment