Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a solution that accepts an input identifying the name of a CSV file, for example, input1.csv. Each file contains two rows of comma-separated values.

Create a solution that accepts an input identifying the name of a CSV file, for example, "input1.csv". Each file contains two rows of comma-separated values. Import the built-in module csv and use its open() function and reader() method to create a dictionary of key:value pairs for each row of comma-separated values in the specified file. Output the file contents as two dictionaries.

The solution output should be in the format

{'key': 'value', 'key': 'value', 'key': 'value'} {'key': 'value', 'key': 'value', 'key': 'value'}

Alternatively, if the input is

input2.csv 

then the expected output is

{'d': '400', 'e': '500', 'f': '600'} {'celery': '2.81', 'milk': '4.34', 'bread': '5.63'}

Someone helped earlier and gave this code:

import csv def read_csv(filename): with open(filename, 'r') as csvfile: reader = csv.DictReader(csvfile) rows = [] for row in reader: rows.append(row) return rows filename = input('file name: ') output = read_csv(filename) print(output[0]) print(output[1])

However, this is the output which is not desired:

file name: input1.csv {' a': 'bananas', ' 100': ' 1.85', ' b': ' steak', ' 200': ' 19.99', ' c': ' cookies', ' 300': ' 4.52'} Traceback (most recent call last): File "C:\Users\tareq\OneDrive\Documents\Python\Third attempt study plan\practice test 2\open_csv_file.py", line 14, in print(output[1]) IndexError: list index out of range

Process finished with exit code 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Optimization And Data Science Trends And Applications 5th Airoyoung Workshop And Airo Phd School 2021 Joint Event

Authors: Adriano Masone ,Veronica Dal Sasso ,Valentina Morandi

1st Edition

3030862887, 978-3030862886

More Books

Students also viewed these Databases questions

Question

Perform an Internet search. Discuss a company that uses EPLI.

Answered: 1 week ago

Question

How do you feel about employment-at-will policies? Are they fair?

Answered: 1 week ago