Question

The file ‘classes.txt’ contains an unknown number of lines. Each line contains two words. The first...

The file ‘classes.txt’ contains an unknown number of lines. Each line contains two words. The first word is the course number (‘CS021’, for example) and the second word is the instructor’s last name (Eddy). There are no duplicate courses in the file.

On the other hand, an instructor may teach more than one course. Write a code segment that opens the file, loads the key/value pairs into a dictionary and then closes the file.

No documentation or exception handling is required.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
filename = "classes.txt"
d = dict()
file = open(filename,"r")
for line in file:
    strings = line.split(" ")
    d[strings[0].strip()] = strings[1].strip()
print(d)

Note: Please comment below if you have any doubtsPlease up vote the solution if it helped. Thanks!

Add a comment
Know the answer?
Add Answer to:
The file ‘classes.txt’ contains an unknown number of lines. Each line contains two words. The first...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that loads a file called "sample.txt" in read mode, reads its content, and...

    Write a program that loads a file called "sample.txt" in read mode, reads its content, and closes the file. Use exception handling to catch any errors. 2. Compute the letter and punctuation distribution in the file. That is, output the number of a’s, the number of b’s, etc. and the number of commas, dashes, and periods. Ignore case when computing this, so ‘A’ and ‘a’ are the same letter. Use lists. 3.   Compute the number of words in the file....

  • A file named "numbers.txt" contains an unknown number of lines, each consisting of a single positive...

    A file named "numbers.txt" contains an unknown number of lines, each consisting of a single positive integer. Write some code that reads through the file and stores the largest number read in a variable named "maxvalue". Use Python

  • Python 3.7 Coding assignment This Program should first tell users that this is a word analysis...

    Python 3.7 Coding assignment This Program should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file....

  • Pythong Program 4 should first tell users that this is a word analysis software. For any...

    Pythong Program 4 should first tell users that this is a word analysis software. For any user-given text file, the program will read, analyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time. Ask a user to enter the name of a text file. Using try/except for...

  • In this lab you will write a spell check program. The program has two input files:...

    In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the document to be spellchecked. The program will read in the words for the dictionary, then will read the document and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is or type in a replacement word and add...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT