Python : pickle.load() only returns the first value of the file












0















I'm creating a very basic program that stores a dictionary of words translated from English to French, for example :



I'm storing all those values in a file that I append and read using the pickle function.



The problem is that when I use pickle.load to read the file and then print it, only the first value is returned.



I can't see where is my error, and I've been looking everywhere without getting any answer.
Thanks in advance.



import pickle
import os

clear=lambda:os.system("cls")

def pause():
input("Press ENTER to continue.")

def print_dictionary(dct):
print("Dictionary (English / French)")
for wordenglish, wordfrench in dct.items():
print("{} : {}".format(wordenglish, wordfrench))

dictionary={}
for loop in range(3):
wordEnglish=input("Enter the word in English : ")
wordFrench=input("Enter the word in French : ")
pause()
clear()
print("Saving ...")
dictionary[wordEnglish]=wordFrench
with open("data","ab") as file:
pickler=pickle.Pickler(file)
pickler.dump(dictionary)
print("Saved !")
pause()
with open("data","rb") as file:
unpickler=pickle.Unpickler(file)
dictionary_get=unpickler.load()
print_dictionary(dictionary_get)
pause()


For example, if I enter "Fire","Feu" / "Water","Eau" / "Mud","Boue", the only value I will get will be "Fire","Feu".










share|improve this question























  • Because you save each item separately inside the loop, overwriting the pickle file each time.

    – Daniel Roseman
    Nov 24 '18 at 21:51













  • @DanielRoseman no they are using 'ab' mode. The problem is that .load won't keep trying to load once it deserializes an object. You have to keep calling .load

    – juanpa.arrivillaga
    Nov 24 '18 at 21:52













  • However, why are you progressively dumping incremental versions of your dict?

    – juanpa.arrivillaga
    Nov 24 '18 at 21:53











  • @DanielRoseman No, because when I open the data file I can clearly see all the values are here.

    – Nitroxone
    Nov 24 '18 at 21:53











  • What do you mean by "keep calling .load" ? @juanpa.arrivillaga As for dumping incremental versions of my dict, it's just because I don't know any other way to do this ...

    – Nitroxone
    Nov 24 '18 at 21:55


















0















I'm creating a very basic program that stores a dictionary of words translated from English to French, for example :



I'm storing all those values in a file that I append and read using the pickle function.



The problem is that when I use pickle.load to read the file and then print it, only the first value is returned.



I can't see where is my error, and I've been looking everywhere without getting any answer.
Thanks in advance.



import pickle
import os

clear=lambda:os.system("cls")

def pause():
input("Press ENTER to continue.")

def print_dictionary(dct):
print("Dictionary (English / French)")
for wordenglish, wordfrench in dct.items():
print("{} : {}".format(wordenglish, wordfrench))

dictionary={}
for loop in range(3):
wordEnglish=input("Enter the word in English : ")
wordFrench=input("Enter the word in French : ")
pause()
clear()
print("Saving ...")
dictionary[wordEnglish]=wordFrench
with open("data","ab") as file:
pickler=pickle.Pickler(file)
pickler.dump(dictionary)
print("Saved !")
pause()
with open("data","rb") as file:
unpickler=pickle.Unpickler(file)
dictionary_get=unpickler.load()
print_dictionary(dictionary_get)
pause()


For example, if I enter "Fire","Feu" / "Water","Eau" / "Mud","Boue", the only value I will get will be "Fire","Feu".










share|improve this question























  • Because you save each item separately inside the loop, overwriting the pickle file each time.

    – Daniel Roseman
    Nov 24 '18 at 21:51













  • @DanielRoseman no they are using 'ab' mode. The problem is that .load won't keep trying to load once it deserializes an object. You have to keep calling .load

    – juanpa.arrivillaga
    Nov 24 '18 at 21:52













  • However, why are you progressively dumping incremental versions of your dict?

    – juanpa.arrivillaga
    Nov 24 '18 at 21:53











  • @DanielRoseman No, because when I open the data file I can clearly see all the values are here.

    – Nitroxone
    Nov 24 '18 at 21:53











  • What do you mean by "keep calling .load" ? @juanpa.arrivillaga As for dumping incremental versions of my dict, it's just because I don't know any other way to do this ...

    – Nitroxone
    Nov 24 '18 at 21:55
















0












0








0








I'm creating a very basic program that stores a dictionary of words translated from English to French, for example :



I'm storing all those values in a file that I append and read using the pickle function.



The problem is that when I use pickle.load to read the file and then print it, only the first value is returned.



I can't see where is my error, and I've been looking everywhere without getting any answer.
Thanks in advance.



import pickle
import os

clear=lambda:os.system("cls")

def pause():
input("Press ENTER to continue.")

def print_dictionary(dct):
print("Dictionary (English / French)")
for wordenglish, wordfrench in dct.items():
print("{} : {}".format(wordenglish, wordfrench))

dictionary={}
for loop in range(3):
wordEnglish=input("Enter the word in English : ")
wordFrench=input("Enter the word in French : ")
pause()
clear()
print("Saving ...")
dictionary[wordEnglish]=wordFrench
with open("data","ab") as file:
pickler=pickle.Pickler(file)
pickler.dump(dictionary)
print("Saved !")
pause()
with open("data","rb") as file:
unpickler=pickle.Unpickler(file)
dictionary_get=unpickler.load()
print_dictionary(dictionary_get)
pause()


For example, if I enter "Fire","Feu" / "Water","Eau" / "Mud","Boue", the only value I will get will be "Fire","Feu".










share|improve this question














I'm creating a very basic program that stores a dictionary of words translated from English to French, for example :



I'm storing all those values in a file that I append and read using the pickle function.



The problem is that when I use pickle.load to read the file and then print it, only the first value is returned.



I can't see where is my error, and I've been looking everywhere without getting any answer.
Thanks in advance.



import pickle
import os

clear=lambda:os.system("cls")

def pause():
input("Press ENTER to continue.")

def print_dictionary(dct):
print("Dictionary (English / French)")
for wordenglish, wordfrench in dct.items():
print("{} : {}".format(wordenglish, wordfrench))

dictionary={}
for loop in range(3):
wordEnglish=input("Enter the word in English : ")
wordFrench=input("Enter the word in French : ")
pause()
clear()
print("Saving ...")
dictionary[wordEnglish]=wordFrench
with open("data","ab") as file:
pickler=pickle.Pickler(file)
pickler.dump(dictionary)
print("Saved !")
pause()
with open("data","rb") as file:
unpickler=pickle.Unpickler(file)
dictionary_get=unpickler.load()
print_dictionary(dictionary_get)
pause()


For example, if I enter "Fire","Feu" / "Water","Eau" / "Mud","Boue", the only value I will get will be "Fire","Feu".







python pickle






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 21:47









NitroxoneNitroxone

192




192













  • Because you save each item separately inside the loop, overwriting the pickle file each time.

    – Daniel Roseman
    Nov 24 '18 at 21:51













  • @DanielRoseman no they are using 'ab' mode. The problem is that .load won't keep trying to load once it deserializes an object. You have to keep calling .load

    – juanpa.arrivillaga
    Nov 24 '18 at 21:52













  • However, why are you progressively dumping incremental versions of your dict?

    – juanpa.arrivillaga
    Nov 24 '18 at 21:53











  • @DanielRoseman No, because when I open the data file I can clearly see all the values are here.

    – Nitroxone
    Nov 24 '18 at 21:53











  • What do you mean by "keep calling .load" ? @juanpa.arrivillaga As for dumping incremental versions of my dict, it's just because I don't know any other way to do this ...

    – Nitroxone
    Nov 24 '18 at 21:55





















  • Because you save each item separately inside the loop, overwriting the pickle file each time.

    – Daniel Roseman
    Nov 24 '18 at 21:51













  • @DanielRoseman no they are using 'ab' mode. The problem is that .load won't keep trying to load once it deserializes an object. You have to keep calling .load

    – juanpa.arrivillaga
    Nov 24 '18 at 21:52













  • However, why are you progressively dumping incremental versions of your dict?

    – juanpa.arrivillaga
    Nov 24 '18 at 21:53











  • @DanielRoseman No, because when I open the data file I can clearly see all the values are here.

    – Nitroxone
    Nov 24 '18 at 21:53











  • What do you mean by "keep calling .load" ? @juanpa.arrivillaga As for dumping incremental versions of my dict, it's just because I don't know any other way to do this ...

    – Nitroxone
    Nov 24 '18 at 21:55



















Because you save each item separately inside the loop, overwriting the pickle file each time.

– Daniel Roseman
Nov 24 '18 at 21:51







Because you save each item separately inside the loop, overwriting the pickle file each time.

– Daniel Roseman
Nov 24 '18 at 21:51















@DanielRoseman no they are using 'ab' mode. The problem is that .load won't keep trying to load once it deserializes an object. You have to keep calling .load

– juanpa.arrivillaga
Nov 24 '18 at 21:52







@DanielRoseman no they are using 'ab' mode. The problem is that .load won't keep trying to load once it deserializes an object. You have to keep calling .load

– juanpa.arrivillaga
Nov 24 '18 at 21:52















However, why are you progressively dumping incremental versions of your dict?

– juanpa.arrivillaga
Nov 24 '18 at 21:53





However, why are you progressively dumping incremental versions of your dict?

– juanpa.arrivillaga
Nov 24 '18 at 21:53













@DanielRoseman No, because when I open the data file I can clearly see all the values are here.

– Nitroxone
Nov 24 '18 at 21:53





@DanielRoseman No, because when I open the data file I can clearly see all the values are here.

– Nitroxone
Nov 24 '18 at 21:53













What do you mean by "keep calling .load" ? @juanpa.arrivillaga As for dumping incremental versions of my dict, it's just because I don't know any other way to do this ...

– Nitroxone
Nov 24 '18 at 21:55







What do you mean by "keep calling .load" ? @juanpa.arrivillaga As for dumping incremental versions of my dict, it's just because I don't know any other way to do this ...

– Nitroxone
Nov 24 '18 at 21:55














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462655%2fpython-pickle-load-only-returns-the-first-value-of-the-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462655%2fpython-pickle-load-only-returns-the-first-value-of-the-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen