0% found this document useful (0 votes)
2 views

programming unit 8

The document outlines a Python program that reads a dictionary from an input file, inverts it, and writes the inverted dictionary to an output file. It includes steps for handling file operations with try-except blocks to manage potential errors. The final output consists of key-value pairs formatted as 'key, value' in the output file.

Uploaded by

arebamhenk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

programming unit 8

The document outlines a Python program that reads a dictionary from an input file, inverts it, and writes the inverted dictionary to an output file. It includes steps for handling file operations with try-except blocks to manage potential errors. The final output consists of key-value pairs formatted as 'key, value' in the output file.

Uploaded by

arebamhenk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1. Create an input file with the original dictionary items.

Each item should be on a separate line, with the key and


value separated by a comma. For example:
Input
cow ,white,
Goat black
snake,green

Output
white, cow
black, Goat
green, snake

2. In your Python program, open the input file using the open() function and specify the file mode as "r" for
reading. Use a try-except block to handle any potential file-related errors.

3. Read the contents of the file using the readlines() method, which returns a list of lines. Iterate over each line
and split it at the comma to separate the key and value. Create a dictionary and add each key-value pair to it.

4. Invert the dictionary by creating a new dictionary and swapping the keys and values. Iterate over the original
dictionary using a for loop and add each value as a key in the inverted dictionary, with the corresponding key as
its value.

5. Open an output file using the open() function with the file mode set to "w" for writing. Use a try-except block
to handle any potential file-related errors.

6. Iterate over the inverted dictionary using a for loop and write each key-value pair to the output file. Format
each line as "key, value" and append a newline character at the end.

7. Close both the input and output files using the close() method to free up system resources.
Here's an example code snippet that demonstrates the above steps:

try:
Open input file for reading
with open("input.txt", "r") as input_file:

Read contents of the file


lines = input_file.readlines()

Create original dictionary


original_dict = {}
for line in lines:
key, value = line.strip().split(",")
original_dict[key] = value

Create inverted dictionary


inverted_dict = {value: key for key, value in original_dict.items()}

Open output file for writing


with open("output.txt", "w") as output_file:
Write inverted dictionary to file
for key, value in inverted_dict.items():
output_file.write(f"{key}, {value}\n")

except FileNotFoundError:
print("File not found.")
except IOError:
print("Error reading or writing to file.")
After running the program, you will have an output file ("output.txt") containing the inverted dictionary. Each
line in the output file will represent a key-value pair, with the key and value separated by a comma. For example:
Output
white, cow
black, Goat
green, snake

The program reads the original dictionary from the input file, inverts it, and writes the inverted dictionary to the
output file. It handles potential file-related errors using try-except blocks.

You might also like