0% found this document useful (0 votes)
11 views

Python Code

The document defines several functions for analyzing grocery store item data from a text file. It opens the file, creates a dictionary with item names and frequencies, and defines functions to: 1) Print the full item list and frequencies in a formatted table 2) Return the count of a specific item name passed to the function 3) Write the dictionary data to an external file to create a histogram graph

Uploaded by

55pttqbcv9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Python Code

The document defines several functions for analyzing grocery store item data from a text file. It opens the file, creates a dictionary with item names and frequencies, and defines functions to: 1) Print the full item list and frequencies in a formatted table 2) Return the count of a specific item name passed to the function 3) Write the dictionary data to an external file to create a histogram graph

Uploaded by

55pttqbcv9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import re

import string
import os

"""
def printsomething():
print("Hello from python!")

def PrintMe(v):
print("You sent me: " + v)
return 100;

def SquareValue(v):
return v * v
"""

groceryDict = {} # creates a dictionary that will be added to


produceList = [] # creates a list that will be added to

# function to open and read the grocer items input file & create a dictionary with
the item and frequency
def ItemList():
fileLocation = r'U:\CornerGrocer\CornerGrocer\Release\InputFile.txt'
with open(fileLocation, 'r') as groceryList:
List = groceryList.readlines() # to read the lines in the file and add
them to the list
List.sort() # to sort the list in alphabetical order

# loop that removes the newline from the items in the list
for produceItems in List:
produceList.append(produceItems.strip('\n'))

# loop to add the item as a key to dictionary and the count


for produceItems in produceList:
if produceItems in groceryDict.keys():
value = groceryDict[produceItems]
groceryDict[produceItems] = value + 1
else:
groceryDict[produceItems] = 1

# function that creates menu option 1 to print the produce item and the frequency
def GroceryListFrequency():
print("List of items and quantities purchased")
ItemList()
Title = '{item:^16}{number:>14}'
print(Title.format(item = 'Produce Item', number = "Total Purchased"))
print()
Line = '{item:>14}{number:.>14}'
for produceItems, count in groceryDict.items(): # loop that prints each item
and counts the frequency
print(Line.format(item = produceItems, number = count))

# function that returns menu option 2 to print one item with the number purchased
for the day
def GroceryItemFrequency(item):
ItemList()
groceryItem = item.capitalize() # makes the first letter capital to read from
file
if groceryItem in groceryDict.keys(): # if statement to get number purchased
of a particular item
num = groceryDict.get(groceryItem) # actually gets the number of that item
and assigns it to num
return num
else:
return 0

# function that creates menu option 3 histogram graph and frequency.dat file
def GroceryHistogram():
ItemList()
with open('frequency.dat', 'w') as graph: # to open the file
for key in groceryDict.keys():
graph.write('{} {}'.format(key, groceryDict.get(key))) # to write the
dict to the file

You might also like