Complete Python Programming Masterclass Beginner to Advanced
Project #2 – Python and .CSV Files
This project will present you with the opportunity to apply the skills that you have learned up to this point with the course.
2.1 READING CONTENT FROM A .CSV FILE
The first part of this project requires you to take the provided .csv file, “example.csv”, and read/print the full content using Python. You will find the file, “example.csv”, attached to this lecture and the previous. Make sure to save the document in the same directory that the you save your Python file(s).
Step 1
Import the “csv” module
Research the csv module by using the “dir” function. Ex. dir(math)
Identify any of the csv attributes/functions that will help you in reading the content of the csv file. Use the “help” function to get more information on these attributes/functions.
Step 2
In order to read the content of the csv file, you will first need to open the file.
You can use the “open” method to perform this operation and assign it to a variable for use with the csv functions.
You can find additional information on the “open” method here: https://docs.python.org/2/library/functions.html#open
Step 3
Using the “reader” function, found in the csv module, you can assign the reader object to a variable to read the file opened using the “open” method.
Step 4
Use a “for loop” to print each row of data in the csv file.
Step 4
Use the “close” method to close the file that you opened in step 2.
2.2 WRITING CONTENT TO A .CSV FILE
The second part of this project will require you to write the steps necessary to write content to the provided .csv file using Python. You will find the file, “example.csv”, attached to this lecture and the previous. Make sure to save the document in the same directory that the you save your Python file(s).
Step 1
Import the “csv” module
Research the csv module by using the “dir” function. Ex. dir(math)
Identify any of the csv attributes/functions that will help you in reading the content of the csv file. Use the “help” function to get more information on these attributes/functions.
Step 2
Using one of the List Collection Types, discussed earlier in the course, create a variable and assign it single record.
There must be four values in the record. Each value must match the headers in the .csv file. (ID, FIRSTNAME, LASTNAME, DEPT)
Step 3
In order to write content to the csv file, you will first need to open the file.
You can use the “open” method to perform this operation and assign it to a variable for use with the csv functions.
You can find additional information on the “open” method here: https://docs.python.org/2/library/functions.html#open
Step 4
Using the “writer” function, found within the csv module, you can assign the writer object to a variable to write to the file you opened earlier.
Step 5
Use the “writerow” function to write the new record, created in step 2, to the “writer” object
Step 6
Use the “close” method to close the file that you opened in step 3.