Question: Help with computer science question CSV file : This is a file containing plain text where each line in the file represents one record of

Help with computer science question

CSV file

: This is a file containing plain text where each line in the file represents one record of

information, and each piece of info in the record is separated by a single comma. Luckily the

values

won't ever contain commas

themselves for this project. But they might start or end with

non visible characters (e.g. space, tab). Thus, you

must remove

the leading and trailing spaces

when you store the data in a dictionary in memory. The very first line is the

header

row, which

names the columns but is

not

part of the data, and therefore should

not

be loaded into

memory.

Note

: the filename extension you use has no effect on the contents; you can edit it and

give it any extension you want without changing the ability of your program. In this project well

be using two kinds of CSV files:

votes.csv

file:

It contains records of votes in the following format:

state, candidate, party, popular_votes, electoral_votes

AL, Johnson, IND, 44467, 0

AL, Stein, IND, 9391, 0

AK, Trump, REP, 163387, 9

VA, Clinton, DEM, 1981473, 13

Note:

the total number of electoral votes in the file is 531 instead of 538 because 7 votes of the

electoral college were not given as promised!

abbreviations.csv

file:

It contains abbreviations of strings in the following format:

abbreviation, full_string

AL, Alabama

AK, Alaska

VA, Virginia

Johnson, Gary Johnson

Stein, Jill Stein

IND, Independent

REP, Republican

DEM, Democratic

For storage efficiency all files contain string abbreviations. When we load the files into memory

we will also be using the same abbreviations for memory efficiency. But when we present results

as output of the program we will be using the full strings.

Candidate

: we will use the following representation for a

candidate

inside our program: a

tuple containing these values in this order. Note that

name

and

party

are string abbreviations,

and

popular_votes

and

electoral_votes

are

integers.

candidate = (name, party, popular_votes, electoral_votes)

Database1

: a database of votes can store all candidates votes for each State. Obviously, a

candidates name may get reused in different States, but never in the same State. Not all

candidate names appear in every State.

Our database

must be a dictionary

whose

keys are

States

, and whose

values are lists of candidate values

. Only candidates with

positive number

of

popular_votes

may be present in a State. Empty lists are

not allowed

as values. Candidates in the

same State must be stored

alphabetically

by their abbreviated name (hint: use

insert

instead of

append

when adding items to the list because you cant use sorting functions).

votes_db = {

AL: [(Johnson, IND, 44467, 0), (Stein, IND, 9391, 0)],

AK: [(

Trump, REP, 163387, 9

)],

VA: [(

Clinton, DEM, 1981473, 13

)]

...

...

...

}

Database2

: a database of abbreviations will store all abbreviations as a dictionary whose

keys are the abbreviated strings and whose values are the equivalent full strings, like in the

following sample:

abbr_db = {

AL: Alabama,

AK: Alaska,

DEM:

Democratic,

REP: Republican,

Trump: Donald Trump

...

...

...

}

Functions

read_votes(filename)

Description

: It reads votes from a file and creates in memory a dictionary of

database1

type

Parameters

: a filename of

a

CSV formated votes file

a

s described above

Return value

: the dictionary object created or False for any kind of error

Examples

:

db = read_votes(votes.csv)

db is a database1 dictionary

db = read_votes(file_with_wrong_format)

db = False

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!