0

My text file looks like so (Number of goals each person scores at different dates):

1/23/15 Jack G2
3/15/15 Sally G5
1/23/12 Jack G1
3/15/14 Sally G3

What I want to do is turn that file into a dictionary, I have done:

Goal = {}
with open("(goals_per_person.txt") as goal:
    for line in bill:
        (name,score) = line.split()
        Goals_per_person[int(name)] = goals

But I'm not sure, how I can ignore the date because it's not needed. After I get the respective goal per person i want to add that onto each other and print it. So add the values of the same key

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
  • Welcome to SO! Check out the [tour]. It seems like you're asking two separate questions here: 1) how to ignore the date, 2) how to add to a dict value instead of overwriting. Both of those have already been asked: [Ignore part of a python tuple](https://stackoverflow.com/q/9532576/4518341), [Trying to split strings into key:value pairs and sum the values](https://stackoverflow.com/q/61488191/4518341). As well your existing code doesn't seem to work. Please ask one question at a time. See [ask] for more advice. – wjandrea May 20 '20 at 16:49

1 Answers1

2

A simple solution is to just disregard the first column of the split, throwing the value into a dummy variable

dummy, name, score = line.split()

Instead of dummy, a common convention is to use _ as the variable, or use that as a prefix (_dummy) to indicate you won't use it.

As an FYI, you will run into the other problems

  • name is not an integer
  • Goals_per_person should be Goal
  • You want to put score into the dictionary, not goals
SethMMorton
  • 45,752
  • 12
  • 65
  • 86