Question

PLEASE DO IT IN PYTHON, THANK YOU!

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 wont 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

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

Code:

def read_votes(filename):

try:

f = open(filename)

votes_db = {}

f.readline()

for line in f.readlines():

fields = line.strip().split(",")

if fields[0] not in votes_db:

votes_db[fields[0]] = [(fields[1], fields[2], int(fields[3]), int(fields[4]))]

else:

i = 0

for state in votes_db[fields[0]]:

if fields[1] < state[0]:

break

i+=1

votes_db[fields[0]].insert(i, (fields[1], fields[2], int(fields[3]), int(fields[4])))

return votes_db

except:

return False

def write_votes(db, filename):

try:

if len(db)==0 or type(db) is not dict:

return False

f = open(filename, 'w')

for country in db:

for l in db[country]:

f.write(country)

for e in l:

f.write(","+str(e))

f.write("\n")

f.close()

return True

except:

return False

Add a comment
Know the answer?
Add Answer to:
PLEASE DO IT IN PYTHON, THANK YOU! CSV file: This is a file containing plain text...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses...

    According to Wikipedia , a comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A company has text data that is not...

  • Python Assignment In this assignment, you will use Pandas library to perform analysis on the dataset stored in the following csv file: breast-cancer-wisconsin.csv. Please write script(s) to do the fol...

    Python Assignment In this assignment, you will use Pandas library to perform analysis on the dataset stored in the following csv file: breast-cancer-wisconsin.csv. Please write script(s) to do the following: 1. Read the csv file and covert the dataset into a DataFrame object. 2. Persist the dataset into a SQL table and a JASON file. • Write the content of the DataFrame object into an SQLite database table. This will convert the dataset into a SQL table format. You can...

  • Python Help Please! This is a problem that I have been stuck on.I am only suppose...

    Python Help Please! This is a problem that I have been stuck on.I am only suppose to use the basic python coding principles, including for loops, if statements, elif statements, lists, counters, functions, nested statements, .read, .write, while, local variables or global variables, etc. Thank you! I am using python 3.4.1. ***( The bottom photo is a continuation of the first one)**** Problem statement For this program, you are to design and implement text search engine, similar to the one...

  • in Python Project 5: Payroll (Part 1) CS 1410 Background In this project you will implement...

    in Python Project 5: Payroll (Part 1) CS 1410 Background In this project you will implement a simple payroll system. For the first part of the assignment, you will submit a UML class diagram. The hypothetical company we are considering has 3 classifications of employees: 1. Hourly 2. Salaried 3. Commissioned There are 24 pay periods per year; 1/24th of a salary is paid each pay period to employees who receive a salary. We won't worry about taxes and other...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT