I'm writing a python script that reads a players name and stats from a sentence in a .txt file, then updates their stats within a dictionary and then prints out their average stats. I'm having trouble with assigning multiple values to the same 'player' key, as well as getting the logic below it to correctly update the player stats. The .group part is giving me trouble too. How can I do this?
import re, sys, os, math
if len(sys.argv) < 2:
sys.exit("Usage: %s filename" % sys.argv[0])
filename = sys.argv[1]
if not os.path.exists(filename):
sys.exit("Error: File '%s' not found" % sys.argv[1])
line_regex = re.compile(r"^(\w+ \w+) batted (\d+) times with (\d+) hits and (\d+) runs")
line = [line.strip() for line in open(filename)]
f = open (filename)
playerStats = {'players': [atBats[0, hits0, avgs]0]}
for players in playerStats:
player = line.group(1)
atBat = line.group(2)
hit = line.group(3)
if player in playerStats:
playerStats[player][1] playerStats[player][0] += atBat
playerStats[player][2] playerStats[player][1] += hit
if player not in players:
player = line.group(1)
playerStats[player][1] playerStats[player][0] = line.group(2)atBat
playerStats[player][2] playerStats[player][1] = line.group(3)hit
avgs = 0
else:
playerStats[player][0] = player
playerStats[player][1] playerStats[player][0] = atBat
playerStats[player][2] playerStats[player][1] = hit
playerStats[player][3] playerStats[player][2] = 0
for player in players:
avgs[player] = round(float(hits[player])/float(atBats[player]), 3)
print "%s: %.3f" % (player, avgs[player])
Traceback (most recent call last): File "ba.py", line 19, in player = line.group(1) AttributeError: 'list' object has no attribute 'group'