Reputation: 333
I am trying to save a list of multiple dictionaries in a pickle file and by able at any time to store a new appended dictionary in the list and save it in the pickle file and then load the pickle file as a list of dictionaries. So far, the code behaves as follows:
The first time I try to append a dictionary in the list, the pickle file looks like this (the data is saved succesfully):
Running info()......
===================================================
Existing data: []
===================================================
Running save_score()......
Inside try: {}
============================================
Existing data: [{}]
New data: {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}
Saved Data: [{}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}]
===================================================
The second time I try to append a dictionary in the list, the pickle file looks like this:
Running info()......
===================================================
Existing data: [[{}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}]]
===================================================
Running save_score()......
Inside try: [{}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}]
============================================
Existing data: [[{}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}], [{}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}]]
New data: {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}
Saved Data: [[{}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}], [{}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}], {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}]
===================================================
As you can see (the values in the dictionary are not a concern in the example above), instead of appending to the existing list, everytime, a new list inside the list is created. So far, I have not managed to fix this and get the expected output.
The expected output should be:
Saved Data: [{}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}, {}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}]
Instead, the output is:
Saved Data: [[{}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}], [{}, {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}], {'ComputerName': 'computer name', 'PlayerName': 'user 1', 'ComputerScore': 3, 'PlayerScore': 1}]
Code is as follows:
import pickle
data = []
computer_name = "computer name"
username = "user 1"
computer_score = 3
human_score = 1
def save_score():
try:
existing_data = pickle.load(open("w6_project.p", "rb"))
print("Inside try: {}".format(existing_data))
data.append(existing_data)
except (OSError, IOError) as e:
pickle.dump({}, open("w6_project.p", "wb"))
new_data = {"ComputerName": computer_name, "PlayerName": username, "ComputerScore": computer_score, "PlayerScore": human_score}
print("============================================")
print("Existing data: {}".format(data))
data.append(new_data)
print("New data: {}".format(new_data))
pickle.dump(data, open("w6_project.p", "wb")) # Save the data into a pickle file
print("Saved Data: {}".format(data))
print("===================================================")
pass
def info():
try:
existing_data = pickle.load(open("w6_project.p", "rb"))
data.append(existing_data)
except (OSError, IOError) as e:
pickle.dump({}, open("w6_project.p", "wb"))
print("===================================================")
print("Existing data: {}".format(data))
print("===================================================")
pass
def main():
print("Running info()......\n")
info()
print("\nRunning save_score()......\n")
save_score()
if __name__ == "__main__":
main()
Upvotes: 0
Views: 28
Reputation: 142909
Problem is that you have two lists - data
and existing_data
- and you join them in wrong way. Using append()
you put one list inside another list. You should use extend()
to join them
data.extend(existing_data)
or shorter
data += existing_data
But if you add new data after loading pickle then you should simply assing directly to variable
data = pickle.load(...)
EDIT: Minimal working example
import pickle
computer_name = "computer name"
username = "user 1"
computer_score = 3
human_score = 1
def save_score():
try:
data = pickle.load(open("w6_project.p", "rb"))
print("Inside try: {}".format(data))
except (OSError, IOError) as e:
data = []
new_data = {"ComputerName": computer_name, "PlayerName": username, "ComputerScore": computer_score, "PlayerScore": human_score}
data.append(new_data)
pickle.dump(data, open("w6_project.p", "wb")) # Save the data into a pickle file
print("============================================")
print("Existing Data: {}".format(data))
print("New Data: {}".format(new_data))
print("Saved Data: {}".format(data))
print("Length Data: {}".format( len(data) ))
print("===================================================")
def info():
try:
data = pickle.load(open("w6_project.p", "rb"))
except (OSError, IOError) as e:
data = []
print("===================================================")
print("Existing Data: {}".format(data))
print("Length Data: {}".format( len(data) ))
print("===================================================")
def main():
print("Running info()......\n")
info()
print("\nRunning save_score()......\n")
save_score()
if __name__ == "__main__":
main()
Upvotes: 1