Average Baseball Player Salary 1985 – 2014

The data is from seanlahman.com.

averagebaseballplayersalaries

C# Console Application written to parse the csv dataset and create json output for a google charts line graph.

string line_player;
System.IO.StreamReader fileCandid =
    new System.IO.StreamReader(@"C:\BASEBALL\Salaries.csv");
Dictionary<string, long> dictYear_Sal = new Dictionary<string, long>();
Dictionary<string, long> dictYear_Count = new Dictionary<string, long>();
while ((line_player = fileCandid.ReadLine()) != null)
{
    Console.WriteLine(line_player);
    string[] parts = line_player.Split(',');
    try
    {
        //year
        Console.WriteLine(parts[0]);
        //sal
        Console.WriteLine(parts[4]);
        string year = parts[0];
        int salary = int.Parse(parts[4]);
 
        if ( dictYear_Sal.ContainsKey(year) )
        {
            long existing_salary = dictYear_Sal[year];
            existing_salary += salary;
            dictYear_Sal[year] = existing_salary;
 
            long e_count = dictYear_Count[year];
            e_count += 1;
            dictYear_Count[year] = e_count;
        }
        else 
        {
            dictYear_Sal.Add(year, salary);
 
            dictYear_Count.Add( year,1);
        }
    }
    catch (Exception x)
    { }
}
var list = dictYear_Sal.Keys.ToList();
list.Sort();
string output = "";
foreach (var key in list)
{
    output += "['"+key+"', "+ ( dictYear_Sal[key] / dictYear_Count[key] )+"],\r\n"   ;
}
StreamWriter streamWrite;
streamWrite = File.AppendText("C:\\Airplanes\\BASEBALLEWWW.txt");
streamWrite.WriteLine(output);
streamWrite.Close();

Leave a Reply

Your email address will not be published.