2014-04-25 Lab15

posted Apr 28, 2014, 5:34 AM by Samuel Konstantinovich   [ updated Apr 28, 2014, 7:41 AM ]

Attention windows users. If you save files and upload them to stuy, the python files will always generate an internal server error. Use gedit, to change the line ending when you:
1. file -> save-as, 
2. change the 'line ending' dropdown menu from windows to unix/linux
3. upload it to school. 

Attached to the lecture are two files.

1. The lab is described in the lecture notes. This is most important. 
2. Attached is py file with some runnable examples,
3. Attached is the data file, it is stored with windows newlines. Open this file in gedit, and change the line endings as outlined above.
4. You must make a new py file, LAB15.py , or LAB15Fileprocessing.py where you will write the solutions to problems 1-4.
The lecture notes have important information about the file format, and hints on what you should do.

Data file format: 
The csv : 'Rk,Year,Tm,Lg,G,W,L,Ties,W-L%,Finish,Playoffs,R,RA,BatAge,PitchAge,#Bat,#Pitch,Top Player,Managers\n2,2012,New York Yankees,AL East,162,95,67,0,0.586,1st of 5,Lost ALCS (4-0),804,668,32.7,30.3,45,23,R.Cano (8.5),Joe Girardi (95-67)\n'

is a representation of this table:
Rk   Year    Tm                  Lg        G     W    L...
2    2012    New York Yankees    AL East   162   95   67...
3    2012    New York Yankees    AL East   160   94   66...
4    2012....

You can open the file in a spreadsheet program or text editor. THIS IS NOT REQUIRED, but can help you see the data better.
-you can open it in gedit, to see it unformatted. Opening the file is not a requirement, but can help you understand what it looks like.
-you open it using libre office, BE CAREFUL you will have to make sure to ONLY HAVE 'commas' CHECKED when it asks you how to read it. NOT Spaces, NOT semicolons.

The first line of this file has the 'names' for each column in the format:
'Rk,Year,Tm,Lg,G,W,L,Ties,W-L%,Finish,Playoffs,R,RA,BatAge,PitchAge,#Bat,#Pitch,Top Player,Managers\n'

The next line, and all subsequent lines are actual data in the format::
'2,2012,New York Yankees,AL East,162,95,67,0,0.586,1st of 5,Lost ALCS (4-0),804,668,32.7,30.3,45,23,R.Cano (8.5),Joe Girardi (95-67)\n'


You can split the header to help you find things:
['Rk', 'Year', 'Tm', 'Lg', 'G', 'W', 'L', 'Ties', 'W-L%', 'Finish', 'Playoffs', 'R', 'RA', 'BatAge', 'PitchAge', '#Bat', '#Pitch', 'Top Player', 'Managers\n']

Then split each line afterwards... the index of the name corresponds to the data...
['2', '2012', 'New York Yankees', 'AL East', '162', '95', '67', '0', '0.586', '1st of 5', 'Lost ALCS (4-0)', '804', '668', '32.7', '30.3', '45', '23', 'R.Cano (8.5)', 'Joe Girardi (95-67)\n']

You can see that
'Year' is index 1, and '2012' is index 1.
You can also see that the numbers are strings, and need to be converted to integers for calculations.

Complete these probems:
Your function may assume: fileText is the readlines() from any file with the same format as yankees.csv.
Your function may assume that columnName is a valid header in the file. 

1. Write a function getSum(fileText,columnName) that calculates the sum of any numerical column in a file that has the format of yankees.csv.

This takes a list of strings fileText, and a string columName
print getSum(fileData,'G')   
#this calculates the total number of games played
#since fileData is a list of strings from a file, and 'G' is a column in the file

#2 Write a similar function printAverage(fileText,columnName) that calculates the average of a numerical column in a file that has the format of yankees.csv.
print getAverage(fileData,'W')
#would print the average number of wins by finding the proper index of 'W'

#write a function yearOfMax(fileText,columnName) that tells you which year did the team have the highest value of a specific column:
print yearOfMax(fileData,'L') 
#this would tell you which year the team lost the most games.

#4 Calculate the average win % of the team. You cannot just average the W-L% column, because each year had a different number of games!
Samuel Konstantinovich,
Apr 28, 2014, 5:34 AM
Samuel Konstantinovich,
Apr 28, 2014, 5:34 AM