Python Forum
Reading Baseball Box Scores
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading Baseball Box Scores
#3
Sorry about not including my code... Here is my attempt but as you can see by what it outputs, its pretty sparse.
import os  # Import the os module

from collections import defaultdict


def find_inning_lines(lines):
  """
  Finds the lines containing inning-by-inning runs based on context clues.

  Args:
      lines: A list of strings representing the game data.

  Returns:
      A list of line numbers containing inning runs, or None if not found.
  """
  for i in range(len(lines)):
    line = lines[i].strip()
    if line.startswith("R H E") or line == "R H E":  # Check for potential inning headers
      return [i + 2, i + 3]  # Assuming inning data 2 lines after the header
  return None


def parse_game(lines):
  """
  Parses the box score lines for a single game.

  Args:
      lines: A list of strings representing the lines for a game.

  Returns:
      A dictionary containing game data, or None if required data is missing.
  """
  data = {}
  # Extract team names
  try:
    data["away_team"] = lines[1].split()[1]
    data["home_team"] = lines[2].split()[1]
  except IndexError:
    return None  # Handle missing team names

  # Extract team scores (assuming scores are present even if inning data is missing)
  try:
    away_score = int(lines[-3].split()[-3])
    home_score = int(lines[-2].split()[-3])
    data["away_runs"] = away_score
    data["home_runs"] = home_score
  except IndexError:
    return None  # Handle missing scores

  # Extract win/loss
  data["away_win"] = away_score > home_score
  data["home_win"] = not data["away_win"]

  # Extract inning-by-inning runs (handle potential missing lines)
  inning_runs = {"away": [0] * 10, "home": [0] * 10}
  inning_lines = find_inning_lines(lines)
  if inning_lines:
    for i, team_line in enumerate(lines[inning_lines[0]:inning_lines[1]]):
      inning_runs[team_line.split()[0]] = [int(r) for r in team_line.split()[2:-4]]
  data["inning_runs"] = inning_runs
  return data


def process_data(filename):
  """
  Processes the data file and generates requested outputs.

  Args:
      filename: The path to the data file.
  """
  # Team counters
  teams = set()
  team_wins = defaultdict(int)
  team_losses = defaultdict(int)

  # Open and parse games
  with open(filename, "r") as f:
    lines = f.readlines()
    games = []
    start = 0  # Track starting line of each game block
    for i in range(len(lines)):
      if lines[i].startswith("05"):  # Identify game start marker
        games.append(parse_game(lines[start:i + 1]))  # Parse game block
        start = i + 1  # Update start for next game block
      elif lines[i].strip() == "--- end of data ---":  # Check for end of data
        break

  # Process games data (only for games with valid data)
  for game in games:
    if game:  # Check if game data is valid before processing
      teams.add(game["away_team"])
      teams.add(game["home_team"])
      if game["away_win"]:
        team_wins[game["away_team"]] += 1
        team_losses[game["home_team"]] += 1
      else:
        team_wins[game["home_team"]] += 1
        team_losses[game["away_team"]] += 1

  # Generate win-loss matrix
  win_loss_matrix = {}
  for team in teams:
    win_loss_matrix[team] = {"wins": team_wins[team], "losses": team_losses[team]}

  # Save win-loss matrix to file
  with open("MyOutput.dat", "w") as f:
    f.write(f"Teams\tWins\tLosses\n")  # Write header with proper closing quotation mark
    for team, stats in win_loss_matrix.items():
      f.write(f"{team}\t{stats['wins']}\t{stats['losses']}\n")

if __name__ == "__main__":
  # Get filename from user
  while True:
    filename = input("Enter the path to your data file: ")
    if os.path.isfile(filename):
      process_data(filename)
      break  # Exit loop after processing a valid file
    else:
      print("File not found. Please try again.")
HERE IS THIS PROGRAMS OUTPUT (MyOutput.dat):
Teams Wins Losses
Gribouillis write May-19-2024, 06:28 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Messages In This Thread
Reading Baseball Box Scores - by tommythumb - May-15-2024, 05:32 PM
RE: Reading Baseball Box Scores - by Pedroski55 - May-16-2024, 09:13 AM
RE: Reading Baseball Box Scores - by tommythumb - May-19-2024, 06:25 AM
RE: Reading Baseball Box Scores - by Pedroski55 - May-21-2024, 09:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Calculating BLEU scores in Python kg17 1 2,673 Jan-12-2021, 08:26 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020