Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You should download batting.csv and place it in the same directory as your Python code. Your job will be to write a Python program that

You should download batting.csv and place it in the same directory as your Python code. Your job will be to write a Python program that finds the player ID of the player with the highest total career RBIs of all time. But be careful: your program should work with any similarly formatted CSV file!

Data Input: Opening the file

First, you will need to read in the data file. You can do this by opening the file using the open function and iterating through each line (remember that you can use either read or readlines; I strongly urge the latter of these). To parse the data contained in each line, you will need to use the split method. We are interested in two columns, 'playerID' and 'RBI'. (Your program should skip the header in the file and completely ignore any lines where the RBI column does not contain a digit.)

You should create an accumulator dictionary called career_rbis that maps each player ID string to an integer representing the total number of RBIs for that player. As you iterate through the file, you should update the career_rbis dictionary. Duplicate entries should be ignored at this point.

Data Processing: Finding the most RBIs

After reading in the data and generating the "career_rbis" dictionary, you should next iterate through the dictionary to find the player with the most career RBIs (thus summing duplicate entries).

You will need two accumulator variables to track both the most RBIs you've seen so far AND the player having that many RBIs. Store an integer representing the highest number of RBIs in a variable named max_rbis and the corresponding player id string in a variable named max_player.

DO NOT try to write your code here. Debugging it here will be very difficult. You should write and test your code on an EWS computer or your own computer.

Using files and directories

When you submit code here, you should use open( 'batting.csv' ) with no directory path. On your own machine things may behave differently. Briefly, the best thing to do is to figure out where Python is running and move your file batting.csv there. Otherwise you can try to find out where your file is located and refer to it directly using the code shared in lecture .

You can also use the smaller batting_test.csv to test your code.

Files:

batting.csv

batting_test.csv

Your submission should include the following variables defined correctly:

career_rbis

max_rbis

max_playercareer_rbis = { } : The satrter code i have downloaded the files on to my computer already, just that I dont know how to code it correctly now by utilizing the starter code and filll in the ( ???) in the starter code. I would greatly apppreciate the help please and thank you!

# Open the file. Call it batting_file. ??? # Read the data from the file using a `for` loop. for line in batting_file.???: line = line.strip() # remove whitespace from the line values = ??? # split the line by commas ',' if values[ 0 ] == 'playerID': continue # skip the header line batter_id = ??? # get the batter_id rbis = ??? # get the RBIs # ignore non-digit RBIs ??? # check if batter_id is in career_rbis if batter_id not in career_rbis: # add it ??? else: # add the RBIs career_rbis[ batter_id ] = ??? # Find the player with the maximum RBIs. This will probably take # several lines of code and some serious thought on your part. ???

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

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago