
For this tutorial, I will go through an example of a college student election for student body president. The tutorial will read from the file that I created in Excel, class_election.csv.
I will assume that the file is in the same directory as the python file that will be created.
So, to start a project with the CSV module, first,
import csv
Next you will want to set a variable to the name of the CSV file.
file_to_open=”class_election.csv”
You need to open the file using a conditional operator, with. You will set the open file to “r” for reading, and then assign the CSV file object to a variable in this case, this_csv_file. Be sure to place a colon at the end of the with statement. The with operator automatically closes the file when the indented commands underneath have finished executing.
with open(file_to_open, ‘r’) as this_csv_file:
For some reason, this command only works if the r is in single quotes.
Then, after indenting, you set another variable, this_csv_reader equal to the CSV reader object with a comma delimiter.
this_csv_reader = csv.reader(this_csv_file, delimiter=”,”)
Next, to get the first row with the column headers,
header = next(this_csv_reader)
print(header)
You should save and run this code to make sure that you have the properly formatted CSV file in the right directory.
If it runs without any errors, you should get the following output,
[student_id, vote]
You will later need to remember that student_id has an index of 0, and vote has an index of 1.
One way to use the CSV module and count row by row is with a for loop.
The for loop will iterate through lines in the CSV reader object previously assigned to this_csv_reader. In this case, the loop iterates through a variable called line. As with other python statements that require the following line to be indented, the statement ends with a colon.
for line in this_csv_reader:
Now it is a good idea to make some variables and initialize them before the with statement. You need to remember that values drawn from a csv file are strings of text. So, to initialize such values, you set them equal to “”.
I also made three different variables to capture the vote totals. This vote count is simplified and hard-coded.
this_student_id=””
this_vote=””
jonesvote=0
dravenvote=0
smithvote=0
Now, indented within the for loop, you need to set this_student_id equal to the first column with an index of 0. Each individual vote, in this case this_vote, will also be in the second column (with index 1) and will be classified by the last name of the candidate. Notably, the number after the line variable in square brackets refers to the column number and not the line number as you might guess. So, line[0] refers to the first column, for example.
this_student_id=line[0]
this_vote=line[1]
Finally, you need to count the votes for each candidate. I just used a series of if statements that are also indented under the for loop.
if this_vote==”Smith”:
smithvote=smithvote+1
if this_vote==”Jones”:
jonesvote=jonesvote+1
if this_vote==”Draven”:
dravenvote=dravenvote+1
Now that the total votes have been tabulated, you will print out the results, and who won the election. You no longer need to indent.
Notably, I turned the number of votes into strings for easier printing.
print (“Draven: ” + str(dravenvote))
print (“Smith: ” + str(smithvote))
print (“Jones: ” + str(jonesvote))
Then I ran through a series of if statements to find the winner.
if dravenvote>smithvote and dravenvote>jonesvote:
print(“Draven Wins!”)
if smithvote>dravenvote and smithvote>jonesvote:
print(“Smith Wins!”)
if jonesvote>dravenvote and jonesvote>smithvote:
print(“Jones Wins!”)
Code for this tutorial and the CSV file that it uses is available at my Github page at, https://github.com/scottcm73/CSV_reading_tutorial.
References
J. Fincher, Reading and Writing CSV Files in Python. RealPython.com. Retrieved from https://realpython.com/python-csv/ on Nov. 18, 2019.
Working with CSV Files in Python. GeeksforGeeks.org. Retrieved from https://www.geeksforgeeks.org/working-csv-files-python/ on Nov. 18, 2019.