Question

readCSV Define the function readCSV(filename)to so it reads the contents of the CSV file named filename...

readCSV

Define the function readCSV(filename)to so it reads the contents of the CSV file named filename and returns a list of dictionaries corresponding to its contents. The CSV file will have a header file with field names. Each dictionary in the returned list of dictionaries must consist of key-value pairs where the key is a field name. The field name must be paired with the corresponding data value from the record.

For example, if sample.csv contains:

a,b,c

1,2,3

4,5,6

7,8,9

then readCSV('sample.csv')must return this list:

[ { 'a':'1', 'b':'2', 'c':'3' },

  { 'a':'4', 'b':'5', 'c':'6' },

  { 'a':'7', 'b':'8', 'c':'9' }

]

Homework Answers

Answer #1

Here is Solution for Above Problem ::

import csv 



def readCSV(filename):
     x =[]
     with open(filename, 'r') as data: 
      for line in csv.DictReader(data): 
        x.append(line)
        
      print("Here is Data :: ",x)


filename=input("Enter CSV file name = ")
print(filename)
readCSV(filename)

where this csv file contains data

a,b,c
1,2,3
4,5,6
7,8,9

Input :

sample.csv

Output :

Enter CSV file name = sample.csv
Here is Data ::  [{'b': '2', 'c': '3', 'a': '1'}, {'b': '5', 'c': '6', 'a': '4'}, {'b': '8', 'c': '9', 'a': '7'}]
Know the answer?
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
Similar Questions
Each part of this lab will use the same input file, named “lab3-in.dat.” This file will...
Each part of this lab will use the same input file, named “lab3-in.dat.” This file will be located in the same directory as the executables (do not specify a path for the file in the Select statements). This file is a roster of animals in a travelling carnival. The record format for the file is as follows: Field Description Length Data Type Name 12 String Gender 1 String Species 15 String The end result of each part of this assignment...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...
Create a header file (lastname_employeerec.h) that defines an employee data structure (sEMPLOYEE) that can be linked...
Create a header file (lastname_employeerec.h) that defines an employee data structure (sEMPLOYEE) that can be linked onto a linked list. The data structure should have the following fields: a. First Name (firstName) b. Last Name (lastName) c. Employee ID (id) d. Start Year (startYear) e. Starting Salary (startSalary) f. Current Salary (currentSalary) g. next Create a library of functions that operate on this data structure. The source code for the functions should be in lastname_employeerec.c and the function prototypes should...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT