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

Reading and Writing CSV Files

Uploaded by

Dilip Kumar Dk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
78 views

Reading and Writing CSV Files

Uploaded by

Dilip Kumar Dk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 3
412212018 CCoursera |Online Courses From Top Universes. Join for Free | Coursera KBacktoWeek3 Lessons Prev Next Project Description: Reading and Writing CSV Files This week's practice project investigated using a list to represent a row of a CSV file, In this project, we will use dictionaries to represent a row of a CSV ile. This dictionaries will then be organized using either alist or a dictionary. Preliminaries: Working on the Project Coding Style In this class, you will be asked to strictly follow a set of coding style guidelines. Good programmers not only get their code to work, but they also write itn 2 way that enables others to easily read and. understand their code, Please read the style guidelines carefully and get into the habit of following them right from the start, A portion of your grade on the project will be based upon coding style, Testing You should always test your code as you write it. Do not try to solve the entire project before running it! Ifyou do this, you will have lots of errors that interact in unexpected ways making your program very hard to debug. Instead, as you write each function, make sure you test ito ensure that itis working properly before moving on to the next function. Throughout this course, we will be using a machine grader (OwlTest) to help you assess your code. You can submit your code to this Owltest page to receive a preliminary grade and feedback on your project. The OwlTest page has a pale yellow background and does net submit your project to Coursera, OwfTest is just meant to allow you to test your project autornatically. Note that trying to debug your project using the tests in OwlTest can be very tedious since they are slow and give limited feedback Instead, we strongly suggest that you first test your program using your own tests. Also, note that each ‘OwlTest link is specific to a particular project. You need to come back to this page and click the link above to ensure that you are running the tests for this project. When you are ready to submit your code to be graded formally, submit your code to the assignment page for this project. You will be prompted to open a tool which will take you to the Coursera LTITest page. Note that the Coursera LTITest page looks similar to the OwiTest page, but they are not the same! The CourseraLT! Test page has a white background and does submit your grade to Coursera, Project: Reading and Writing CSV Files We suggest you start by reviewing the Python documentation on the csv module. Your code will rely in the DictReader‘) and DictWriter() methods from this module, We have provided the following template lat you can use to get you started, Note this project should be implemented in desktop Python since CodeSkulptor3 does not implement the esv module, hitps:luwn.coursera.orgleamipytnon-analysis/supplementiXsD@Qiprojactdescription-eading-and-wrting-esv-los ue arzar2018 CCoursera |Online Courses From Top Universes. Join for Free | Coursera This template includes the signatures (name, parameters, and docstrings) for all of the functions that you will need to write. The code however, simply returns some arbitrary value no matter what the inputs are, so you will need to modify the body of the function to work correctly. You should nat change the signature of any of the functions in the template, but you may add any code that you need to. You can also download all of the files used by OwlTest when testing your code as a zip file Problem 1: Reading the field names from a CSV file First, you will write a function called read_csv_fieldnames that takes the name of a CSV file and returns a list of the fleld names from that file. This function assumes that the first row of the CSV file contains the field names. As CSV files can use different separator characters and quote characters, this function takes those characters as input and uses them to properly parse the CSV file, Here is the signature of the read_esv_fieldnames function: 1 def read_csv_fieldnanes(Filenane, separator, quote): 3 inputs 4 Filenase - nane of CSV file 5 separator = character that separates fields 6 quote = character used to optionally quote Fields 7 output 8 1 List of strings corresponding to the fSelé nanes in 5 the given CSV file. You need to write the code that implements this function. Hint 4. You do not actually need to read any of the rows of the CSV fle to obtain the fiakinames. 2, Ifyou are having trouble implementing this funct documentation for the esv module. n, we suggest that you review this piece of Problem 2: Reading a CSV file into a list of dictionaries Next, you will write a function called read_csv_as_list_dict that takes the name of a CSV file and returns the data within the file asa list of dictionaries. Each item in the list corresponds to a row in the SV file, The dictionaries within the list map the fleld names to the column values for that row. As CSV files can use different separator characters and quote characters, this function takes those characters as input and uses them to properly parse the CSV file. Here is the signature of the read_esv_as_list_dict function: 1 def read_csv_as_list_dict(Filenane, separator, quate): 3 inputs 4 Filenase - nane of CSV file 5 separator = character that separates fields 6 quote = character used to optionally quote Fields 7 output 8 Returns a list of dictionaries where each item in the List 9 corresponds to a row in the CSV file. The dictionaries in the 2 List map the field nanes to the field values for that row. You need to write the code that implements this function. Problem 3: Reading a CSV file into a dictionary of dictionaries Next, you will write a function called read_csv_as_nested_dict that takes the name of a CSV fle and returns the data within the file as a dictionary of dictionaries. Each key-value pair in the outer dictionary corresponds to a row in the CSV file. The keys in that dictionary are the values of a header hitps:luwn.coursera.orgleamipytnon-analysis/supplementiXsD@Qiprojactdescription-eading-and-wrting-csv-les 28 arzar2018 CCoursera |Online Courses From Top Universes. Join for Free | Coursera column in the table, The function takes the name of that header column as the input keyfield. Ifa key appears multiple times in column corresponding to keys ield, the last row containing the key is used to create the dictionary used as the corresponding value.The inner dictionaries within the outer dictionary map the field names to the column values for that row. As CSV files can use different separator characters and quote characters, this function takes those characters as input and uses them to properly parse the CSV file, Note that the key-value pair for keyfield should be in the inner dictionaries, even though its value is used as the key in the outer dictionary. Here is the signature of the read_esv_as_nested_dict function 1 def read_csv_as_nosted dict(filenane, Keyfield, separator, quote) 3 inputs 4 fFilenase ~ nane of CSV file 5 keyfield - field to use as key for rows & Separator - character that separates. fields 7 quote character used to optionally quote fields 8 oxtput 9 eturns a dictionary of dictionaries where the outer dictionary 18 naps the value in the key field to the corresponding row in the u CSV file. The inner dictionaries map the field nanes to the n field values for that rom. a You need to write the code that implements this function, Problem 4; Writing a list of dictionaries to a CSV file Finally, you will write a function called write_csv_from_list_dict. This function takes a table structured as a list of dictionaries (as if read by read_csv_as_list_dict) and writes itto the file named by the £i1ename input. The function also takes a list of field names, fieldnames, as input in order to make sure the fields appear in the appropriate order (as specified by the order of the list fieldnames) in the CSV file, The function also takes a separator character and a quote character that should be used when writing the file. All non-numeric fields should be quoted using the specified quote character. Here is the signature of the write_csv_from_list_dict function: 1 def write csv_from_List_dict(filenane, table, flelenaees, separator, quote): 3 inputs 4 filenace - nane of CSV file 5 table _ List of dictionaries containing the table to write 6 Fleldranes - list of strings corresponding to the Field nanes in order 7 separater- character that separates fields 8 quote character used to optionally quote fields 3 ovtput: Fr Writes the table to a CSV file with the nane filename, using the u given fieldnanes, The CSV file should use the given separator and a uote characters, ALL non-nureric fields will be quoted. You need to write the code that implements this function, Hints: 4. Do not forget to open the CSV file for writing. 2, Be sure to review the csv module documentation to learn how to write a CSV file, 3, Many of the options that you can use for reading CSV files are also valid when writing CSV files. Y Complete hitps:luwn.coursera.orgleamipytnon-analysis/supplementiXsD@Qiprojactdescription-eading-and-wrting-esv-los 38

You might also like