Not loading entire txt file into dictionary
Question: Im a python noob to get that out of the way and I am writing these functions
using the OOP method just a FYI. My save_roster function works correctly, it
saves all of the dictionary player roster to my text file 'roster'. I
confirmed this by looking in the text file making sure it is all there. Now
when I go to load_roster function it only loads the first key and value and
none of the rest and I cant figure out why. Any help as to how I can load the
entire dictionary or what I am doing wrong would be greatly appreciated.

    
    
    def save_roster(player_roster):
        print("Saving data...")
        file = open("roster.txt", "wt")
        import csv
        w = csv.writer(open("roster.txt", "w"))
        for key, val in player_roster.items():
            w.writerow([key, val])
        print("Data saved.")
        file.close()
    
    def load_roster(player_roster):
        print("Loading data...")
        import csv
        file = open("roster.txt", "rt")
        for key, val in csv.reader(file):
            player_roster[key] = eval(val)
            file.close()
            print("Data Loaded Successfully.")
            return (player_roster)
    


Answer: Your `return (player_roster)` statement is inside of the `for` loop, which
means it only reads the first line before returning. You need to put the
statement outside the loop like so:

    
    
    def load_roster(player_roster):
        print("Loading data...")
        import csv
        file = open("roster.txt", "rt")
        for key, val in csv.reader(file):
            player_roster[key] = eval(val)
        file.close()
        print("Data Loaded Successfully.")
        return (player_roster)
    

