menu
bartleby
search
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?

Question

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

check_circleAnswer
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

Program:

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:

    print(each_val,":",dictionary[each_val])

Step 3

dictionary.txt:

...
fullscreen

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

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

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...

question_answer

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> :: = ...

question_answer

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,

question_answer

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...

question_answer

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...

question_answer

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 ...

question_answer

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...

question_answer

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...

question_answer

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?