Question
For Python 3.7+. Step 1: Copy and paste this code. Get familiar with what it does. You will use this as a base: def loadItems():
For Python 3.7+.
Step 1: Copy and paste this code. Get familiar with what it does. You will use this as a base:
def loadItems():
fp = open("A1input.txt")
itemsList = []
for line in fp.readlines():
itemsList.append(line.strip().split(","))
fp.close()
return itemsList
def displayItems(itemsList):
for item in itemsList:
print(item)
itemList = loadItems() # invoke function to read the file into a list, return the list
displayItems(itemList) # takes list as input and prints it
Step 2: Create a .txt in the same directory called A1inputs.txt. Copy the following in:
B001,book,Patriot Games, 15.95
B002,book,Origin, 19.95
C001,cloth,Armani Suit, 800.00
B003,book,Animal Farm, 9.97
B004,book,Grant, 22.50
E001,elect,EyePhone 10,795.00
E002,elect,First Alert Smoke Alarm, 29.95
F001,food,Moose Drool Ale 6-pack, 9.95
C002,cloth,Pants, 39.95
B005,book,Prairie Fires, 18.95
E003,elect,Sony Portable Radio, 15.00
C003,cloth,Vasque Hiking Boots, 109.00
C004,cloth,Wool Hat, 14.00
F002,food,Jumbo shrimp, 12.50
E004,elect,HP Laptop, 350.00
Step 3: After you did Step 1 and 2, I want you to finish the rest of my assignment, which is to create 2 additional functions and then call them in the global code. The first function should build a "reverse" dictionary consisting of each item type as key and all items in that category as the value. The second function should create an output file and write each item to it by iterating through the dictionary and writing by category + item.
Additional instructions for the first function I want you to make:
Additional instructions for the second function I want you to make:
Step 4: Use the following global code to call your two functions after you're done:
DS = buildDict(itemList) # builds and returns the reverse dictionary from the input list
writeFile(DS) # creates output file and writes records to it
Reverse Dictionary The 'buildDict(- )' function reads each inventory item from the input list and takes one of the following actions: 1. if the 'category of the item read is not already in the dictionary, that category is added as a key and the inventory number, description, and price are made into a list and inserted as a value of that category's key 2. if the category' of the item read is already in the dictionary, the inventory number, description, and price are made into a list and appended to that key's value. The resulting dictionary has as many entries as categories. Each entry is a list of lists. The master list includes everything for the corresponding category, while each sub-list is the data for one particular inventory item. An example of the dictionary for the 'book'category: {book: [['B001', Patriot Games', 15.95], [B002', 'Origin, 19.95), ... [B006', Future Shock, 8.95]]} Output File The output file is a text file consisting of comma-separated data. The file name you create is "
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started