close search
Hit Return to see all results

How do I get values from a dictionary into a Python code when my values are in a .txt file?


How do I get values from a dictionary into a Python code when my values are in a .txt file?

Step 1

Program instructions:

  • Initialize a dictionary variable named "dictionary" and use open() function to read the ".txt" file.
  • Use the for-each loop along with the split() method to separate the key and value, and save them in the dictionary variable "dictionary".
  • The values from the dictionary can be read by specifying the key for a particular value.
Step 2


dictionary = {}

# use the given code to get values from the file in dictionary

with open("dictionary.txt") as dict_file:

  for line in dict_file:

    (key, value) = line.split()

    dictionary[int(key)] = value

# use the following code to get the value from dictionary

  for each_val in dictionary:


Step 3



Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Our solutions are written by experts, many with advanced degrees, and available 24/7

See Solution
Tagged in


Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add

Q: create a file in c++. In this lab, you will replace the existing functions with more flexible ones, ...

A: Program Instructions:Define the necessary constants and header files.Use displayMessage() function t...


Q: Describe, in English, the language defined by the following grammar in BNF (start symbol is ):

A: Solution:The given grammar is,<S> :: = <A> |<B><C> :: = a | c<A> :: = ...


Q: Is the following distributive law valid or invalid? Prove your answer. x XOR (y + z) = (x XOR y) + ...

A: Answer:Here, the truth table is used to get the answer for each side of the question.First take LHS,


Q: Consider a 32-bit hexadecimal number stored in memory as follows: Address Value 100 2A 101 ...

A: Big-endian:Big-endian is defined as the order in which the most significant value in the sequence is...


Q: Consider the languages below. For each, make a conjecture whether or not it is regular. Then prove y...

A: The given language L = {anbl : n/l is an integer} is not regular language because a DFA cannot be co...


Q: what are some of the challenges that can face a firm to combine “a clear brand vision with a common ...

A: Introduction:The combination of clear brand vision of an organization with a common operating model ...


Q: I have a question about blockchain technologies. The question states "Describe three components of b...

A: Solution:Blockchain technologies:Blockchain is a technology which permits the digital data to be dis...


Q: Hello! How to create a Pynthon function that strips the first letters from all file names in a direc...

A: Program Instructions:Import the os module and create a function named strip_first_letter().Ask the u...


Q: Find a regular expression for the set {anbm : n ≥ 3, m is odd}

A: Regular expression:Given: {an bm: n >=3, m is odd}

Sorry about that. What wasn’t helpful?