question archive Part 1 Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file
Subject:Computer SciencePrice:2.87 Bought7
Part 1
Modify your program from Learning Journal Unit 7 to read dictionary items from a file and write the inverted dictionary to a file. You will need to decide on the following:
Create an input file with your original three-or-more items and add at least three new items, for a total of at least six items.
Part 2
Copy your program from Part 1 and modify it to do the following:
It will be interesting to see if your original dictionary is reversible. If you invert it twice, do you get the original dictionary back?
Include the following in your Learning Journal submission:
Answer:
Input File - dict_in.txt
A:Apple
B:Ball
C:Cat
D:Dog
E:Egg
F:Fish
G:Game
H:Horse
I:India
J:Jug
K:Kite
L:Lemon
M:Monkey
N:Nike
O:Owl
P:Pet
Q:Queue
R:Race
S:Sudden
T:Tale
U:Umbrella
V:Venice
W:Wealth
X:Xylophone
Y:Yacht
Z:Zebra
create_dict.py
f = open("dict_in.txt", "r") #opening the input file in read mode
dict = {} #creating an empty dictionary
for i in f.readlines(): #readin line by line from the file
temp=i.split(":") #spliting the items by ":" and appending them to an array
#(if there is some other separator used instead of ":" then
# that should be put inside split() function)
# the first element in the array is the key and second
# element is the value
dict[temp[0]]=temp[1].strip("\n") #adding the key value pair to the dictionary
## the second element will actually have a "\n" with it
## so the value string needs to be stripped of it
f.close() ##closing the file
##print(dict)
inv_dict = {v: k for k, v in dict.items()} #inverts the dictionary
f1=open("dict_out1.txt","w+") #creating output file
for key,value in inv_dict.items(): #iterating over the inverted dictionary
#print(key,value)
f1.write(str(key)+":"+str(value)+"\n") #writing the key,value pair to the
## output file
f1.close() #closing the file
Output File - dict_out1.txt
Apple:A
Ball:B
Cat:C
Dog:D
Egg:E
Fish:F
Game:G
Horse:H
India:I
Jug:J
Kite:K
Lemon:L
Monkey:M
Nike:N
Owl:O
Pet:P
Queue:Q
Race:R
Sudden:S
Tale:T
Umbrella:U
Venice:V
Wealth:W
Xylophone:X
Yacht:Y
Zebra:Z
create_dict2.py
f = open("dict_out1.txt", "r") #opening the input file in read mode
dict = {} #creating an empty dictionary
for i in f.readlines(): #readin line by line from the file
temp=i.split(":") #spliting the items by ":" and appending them to an array
#(if there is some other separator used instead of ":" then
# that should be put inside split() function)
# the first element in the array is the key and second
# element is the value
dict[temp[0]]=temp[1].strip("\n") #adding the key value pair to the dictionary
## the second element will actually have a "\n" with it
## so the value string needs to be stripped of it
f.close() #closing the file
##print(dict)
inv_dict = {v: k for k, v in dict.items()} #inverts the dictionary
f1=open("dict_out2.txt","w+") #creating output file
for key,value in inv_dict.items(): #iterating over the inverted dictionary
#print(key,value)
f1.write(str(key)+":"+str(value)+"\n") #writing the key,value pair to the
## output file
f1.close() #closing the file
dict_out2.txt
A:Apple
B:Ball
C:Cat
D:Dog
E:Egg
F:Fish
G:Game
H:Horse
I:India
J:Jug
K:Kite
L:Lemon
M:Monkey
N:Nike
O:Owl
P:Pet
Q:Queue
R:Race
S:Sudden
T:Tale
U:Umbrella
V:Venice
W:Wealth
X:Xylophone
Y:Yacht
Z:Zebra
The only difference between program for part 1 and part 2 is input files are different.
And the output file from program 2 as same as the input file from program 1.