#example of using a dictionary to store words and translations from tkinter impo
ID: 3595413 • Letter: #
Question
#example of using a dictionary to store words and translations
from tkinter import filedialog
from tkinter import *
root = Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir = "/",
title = "Select file",
filetypes = (("txt files","*.txt"),("all files","*.*")))
# This will get a file path and name.
print (root.filename)
infile = open(root.filename, "r") #This will open a file with “read” only.
str=infile.read() #This will read all content then save it to a variable ‘str’
print(str) # This will print the content of the file.
#str=infile.read() (1) Make coment this line
#print(str) (2) Make coment this line
# (3) Add from below lines for “TranslationDictionary.py (2)
translationDictionary = dict() # prepare an empty dictionary
print("Dictionary is prepareing! Would you wait!")
#i=1 # for trace, we will make this as comment later.
try: # try below
for line in infile.readlines(): # This will read each line
line = line.strip() # get rid of newline charactes
index = line.find('=') # find the position of = sign,
wordOne = line[0:index] # separate each word_1.
wordTwo = line[index+1:] # separate each word_2.
wordOne = wordOne.strip(' ') # get rid of extra spaces
wordTwo = wordTwo.strip(' ') # get rid of extra spaces
translationDictionary[wordOne] = wordTwo #store in dictionary
#print(i) # trace each line is completed.
#i=i+1
except: # in case of exception
print(root.filename + " can not be processed")
finally: # once you done try,
infile.close() # close file
print("done!")
items = translationDictionary.items() # put all items in a list items
for item in items: # list loop
print(item) # print each
Explanation / Answer
#example of using a dictionary to store words and translations
from tkinter import filedialog
from tkinter import *
root = Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir = "/", title = "Select file", filetypes = (("txt files","*.txt"),("all files","*.*")))
# This will get a file path and name.
print (root.filename)
infile = open(root.filename, "r") #This will open a file with ?read? only.
#str=infile.read() #This will read all content then save it to a variable ?str?
#print(str) # This will print the content of the file.
#str=infile.read() (1) Make coment this line
#print(str) (2) Make coment this line
# (3) Add from below lines for ?TranslationDictionary.py (2)
translationDictionary = dict() # prepare an empty dictionary
print("Dictionary is prepareing! Would you wait!")
#i=1 # for trace, we will make this as comment later.
try: # try below
for line in infile.readlines(): # This will read each line
line = line.strip() # get rid of newline charactes
index = line.find('=') # find the position of = sign,
wordOne = line[0:index] # separate each word_1.
wordTwo = line[index+1:] # separate each word_2.
wordOne = wordOne.strip(' ') # get rid of extra spaces
wordTwo = wordTwo.strip(' ') # get rid of extra spaces
translationDictionary[wordOne] = wordTwo #store in dictionary
#print(i) # trace each line is completed.
#i=i+1
except: # in case of exception
print(root.filename + " can not be processed")
finally: # once you done try,
infile.close() # close file
print("done!")
while True:
englishWord = input("Enter an English word(x toexit): ")
if englishWord == "x":
break
if englishWord in translationDictionary:
print(englishWord, "translates to ", translationDictionary[englishWord])
else:
print(englishWord, "is not in the dictionary. Try other words.")
print("Thank you for using our dictionary. Bye!")
# copy pastable code link: https://paste.ee/p/SEjZD
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.