Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Language: Python Program used: PyCharm Interpreter: 3.6.1 at ~/anaconda/bin/python ----- ----- Starter code: https://pastebin.com/NgrECZQ7 candidates_2016: https://www.dropbox.com/s/oc8ws0qfz01664n/sk_election_candidates_2016.csv?dl=1 votecounts_2016: https://www.dropbox.com/s/ucgaf263q2x3qrm/sk_election_votecounts_2016.csv?dl=1 ----- YOU JUST NEED QUESTION #7 FROM

Language: Python

Program used: PyCharm

Interpreter: 3.6.1 at ~/anaconda/bin/python

-----

image text in transcribed

-----

Starter code: https://pastebin.com/NgrECZQ7

candidates_2016: https://www.dropbox.com/s/oc8ws0qfz01664n/sk_election_candidates_2016.csv?dl=1

votecounts_2016: https://www.dropbox.com/s/ucgaf263q2x3qrm/sk_election_votecounts_2016.csv?dl=1

-----

YOU JUST NEED QUESTION #7 FROM THE STARTER CODE PROVIDED ABOVE.

Question 7 (5 points): Purpose: To practice reading data from a file. Degree of Difficulty: Moderate. File I/O can be frustrating Task: Write a function called read election_files which takes two arguments: a string with the name of a candidates file, and a second string with the name of a vote counts file. Your function should read both files, and return two lists, each list containing the contents of one of the files. Return the two lists as a tuple Details: Remember from the overview that both files are plain-text, tabular files where each value is sep- arated by a comma. We have already written a function in question 5 to process the first Line of each file (i.e. the column headers) Let's work with one file at a time, beginning with the candidates file. Since the first line of the file does not contain any candidate names, you will need to skip it. Call the method readline) once before you begin looping over each line in the file this allows you to start looping over file lines from the second line. For every Line in the file, strip the newline off the end of each line (use the rstrip) method), and split each line into a list. You may want to refer to Chapter 12 (pp. 96-97) of the textbook. Keep in mind that here, the data is separated by commas, not spaces. Each line in the file becomes a list, and the whole file should be stored as a list of lists. Repeat the steps above for the vote count file. The only difference and complication with this file is that vote counts are represented as integers and reading a Line from a file always yields a string. You will need to convert all the vote counts from strings to integers. Assuming you have successfully built a list of lists called votecounts to represent the vote count file, use the following code to quickly make this conversion: for d in votecounts: d[1:len(d)] [int (v) for v in d[1:len (d)]] Return both lists discussed above as a single tuple Testing: Before going on, you want to be sure that your function works properly. The starter file has the testing code, but it will be initially part of a comment. Uncomment the testing for this question while you are working on it and comment it out when you are finished. Assume the two tabular files always contain a matching number of columns; however make sure your function works for any number of parties (i.e. columns)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions

Question

Timeline for final evaluation

Answered: 1 week ago