In: Computer Science
Python Program : simplist form and include comments
Shopping Data by Zip Code
Have you ever noticed that some stores ask you for your zip code before “ringing up” your transaction? Suppose you have been given three (3) text files (named kroger.txt, publix.txt, and ingles.txt) from popular grocery stores, containing zip codes of customers at each store. Additionally, you have been given a file named zipDirectory.txt which contains the zip code and city name (each on its own line). This file will be used as the "dictionary"/"lookup" for zip codes.
Text Files for this problem: Lab7Files.zip
You have been asked to write a Python program to complete the following tasks:
zipDirectory file
11111
City1
22222
public txt file
55555
66666
ingles txt file
77777
88888
99999
22222
44444
kroger.txt
11111
22222
33333
44444
55555
Python3 code:
# opening the zipDirectory.txt file
zipDirectory_filename="zipDirectory.txt"
zipDirectory_file=open(zipDirectory_filename, 'r')
# make a dictionary to store cityname as key and zip code as
value
zipDirectory={}
for line in zipDirectory_file.readlines():
line=line.strip()
# check if line is not empty
if line:
line=line.split()
zipDirectory[line[1]]=line[0]
# opening the kroger.txt file
kroger_filename="kroger.txt"
kroger_file=open(kroger_filename, 'r')
# make a set to store customers from kroger
kroger_data=set({})
for line in kroger_file.readlines():
line=line.strip()
# check if line is not empty
if line:
# adding code in kroger set
kroger_data.add(line)
# opening the publix file
publix_filename="publix.txt"
publix_file=open(publix_filename, 'r')
# make a set to store customers from publix
publix_data=set({})
for line in publix_file.readlines():
line=line.strip()
# check if line is not empty
if line:
# adding code in publix set
publix_data.add(line)
# opening the ingles file
ingles_filename="ingles.txt"
ingles_file=open(ingles_filename, 'r')
# make a set to store customers from ingles
ingles_data=set({})
for line in ingles_file.readlines():
line=line.strip()
# check if line is not empty
if line:
# adding code in ingles set
ingles_data.add(line)
# make a set of all zipcodes
all_zipcodes=set({})
# add codes from kroger, publix, ingles sets into all_zipcodes
set
all_zipcodes.update(kroger_data)
all_zipcodes.update(publix_data)
all_zipcodes.update(ingles_data)
print("Displaying the zip codes from all of the stores:")
for code in all_zipcodes:
# print each code from all stores
print(code)
print("Displaying the zip codes that are in the Kroger file but
are not in the Ingles file")
for code in kroger_data:
# for all code in kroger data check if code not
present in ingles set
if code not in ingles_data:
print(code)
print("Displaying the zip codes that appear in the Publix file
and the Kroger file")
for code in publix_data:
# for all codes in publix check if code also present
in kroger
if code in kroger_data:
print(code)
# get city5 zipcode from zipDirectory dictionary
city5_code=zipDirectory['City5']
# count variable counts number of customers from city5 in all
stores
count=0
# add to count if customer code present in sets of three
stores
if city5_code in kroger_data:
count+=1
if city5_code in ingles_data:
count+=1
if city5_code in publix_data:
count+=1
print("Displaying the total number of shoppers from City5")
print(count)
Output: