Built a script to analyze financial records from a bank and to help modernize a rural town vote-counting process.
import os
import csv
The financial dataset called budget_data.csv is composed of two columns: "Date" and "Profit/Losses".
Whereas, the poll data called election_data.csv comprised of three columns namely: "Voter ID", "County", and "Candidate".
Both datasets are delimited by commas
iterate through each row in data to count total votes and grep a list of candidates
for row in csvreader:
#count the total votes
total_votes += 1
#list of candidates
candidate_list.append(row[2])
iterate through the list of candidates, store unique names in a dictionary and corresponding votes
for i in range(len(candidate_list)):
#create a key for each candidate to store votes casted
if (candidate_list[i]) not in candidate_names:
candidate_names.append(candidate_list[i])
#assign a value of zero to each key (i.e., candidate)
candidate_votes[candidate_list[i]] = 0
#add a value of 1 to each vote counted
candidate_votes[candidate_list[i]] = candidate_votes[candidate_list[i]] + 1
export summary of results to a text file
#text file with results from analysis
with open(os.path.join("analysis", "Summary.txt"), "w") as txtfile:
txtfile.write("Election Results" + "\n")
txtfile.write("-------------------------------------" + "\n")
txtfile.write("Total Votes:" + " " + str(total_votes) + "\n")
txtfile.write("-------------------------------------" + "\n")
#simultaneously iterate each through candidate votes and percentage
for (candidate,votepercent) in zip(candidate_votes,votepercent_list):
#print each unique candidate, their votes and vote percent
txtfile.write(str(candidate)+":" + " "+ str(votepercent) + "%" + " "+ "("+ str(candidate_votes[candidate]) + ")" + "\n")
#write to text file the winner
txtfile.write("-------------------------------------" + "\n")
txtfile.write("Winner:" + " " + str(winner_name) + "\n")
txtfile.write("-------------------------------------" + "\n")
txtfile.close()
Data for this dataset was generated by edX Boot Camps LLC, and is intended for educational purposes only.