2

I made a program that stores some data into a file with Pickle

Here is what the file looks like:

ÄX
(7836)q.ÄX
(13289)q.ÄX
(0928)q.ÄX
(26)q.ÄX
(7893)q.ÄX
(3883)q.ÄX
(1982)q.ÄX

what it is is not important

but when I try to read it with:

data = pickle.load(open("num.txt", "rb"))
print(data)

this is the output:

(7836)

while the expected result is:

(7836)
(13289)
(0928)
(26)
(7893)
(3883)
(1982)

How do I fix this?

rafa_rrayes
  • 155
  • 9
  • 1
    Your expected result doesn't make any sense, it isn't a valid python literal. What, **exactly** were you expecting? – juanpa.arrivillaga Sep 19 '19 at 21:40
  • I am expecting it to print what i dumped, in the case, what you see up there – rafa_rrayes Sep 19 '19 at 21:41
  • Right, that **doesn't make any sense**. What object, **exactly**, do you expect `data` to be? – juanpa.arrivillaga Sep 19 '19 at 21:42
  • 1
    `pickle.load()` expects a pickle format file— created with `pickle.dump()` — but it looks like you may be trying to give it a text file. How was the `num.txt` file created? – martineau Sep 19 '19 at 21:43
  • ```txt_filename = (str(imagename)+'.txt') save = open(txt_filename, "wb")```like this – rafa_rrayes Sep 19 '19 at 21:44
  • then I added data like this: ```pickle.dump(str(number), save)``` – rafa_rrayes Sep 19 '19 at 21:46
  • @martineau the problem is that the OP is showing the pickle file opened in some text editor which is being decoded from the raw bytes. – juanpa.arrivillaga Sep 19 '19 at 21:48
  • Note, you can call `pickle.load` multiple times on your file to get multiple objects out of it. Or store your objects in a container, then serialize the container. But again, your expected output doesn't make any sense, although you likely want one of those two alternatives – juanpa.arrivillaga Sep 19 '19 at 21:48
  • @juanpa.arrivillaga: Yes, I thought that might be the case — and is why I asked how it was created. That would also tell us what to expect `load()` to return. – martineau Sep 19 '19 at 21:52
  • what would be a better way for me to store and reacces data then? – rafa_rrayes Sep 19 '19 at 21:55
  • Check out [this](https://stackoverflow.com/questions/35067957/how-to-read-pickle-file). I tried only @jsbueno's answer so far, worked for me. – B Shmid Jan 26 '22 at 16:51

2 Answers2

0

The following is @jsbueno's answer, worked for me.To be found here +more answers.

Pickle serializes a single object at a time, and reads back a single object - the pickled data is recorded in sequence on the file.

If you simply do pickle.load you should be reading the first object serialized into the file (not the last one as you've written).

After unserializing the first object, the file-pointer is at the beggining of the next object - if you simply call pickle.load again, it will read that next object - do that until the end of the file.

objects = []
with (open("myfile", "rb")) as openfile:
    while True:
        try:
            objects.append(pickle.load(openfile))
        except EOFError:
            break
B Shmid
  • 1
  • 2
0

So I'll explain this , it is simple, the file is:

    ÄX
    (7836)q.ÄX
    (13289)q.ÄX
    (0928)q.ÄX
    (26)q.ÄX
    (7893)q.ÄX
    (3883)q.ÄX
    (1982)q.ÄX

When u give

    print(pickle.load(File1))

The output is:

    (7836)

But when you give

    print(pickle.load(File1))
    print(pickle.load(File1))
    print(pickle.load(File1))

The output is:

    (7836)
    (13289)
    (0928)

So the thing is pickle.load reads only one line at a time so what i suggest is that u find the number of lines of the file, simply by splitting at the '.' just like i've given below and loading line by line using for loop

    f1=open("File1.dat")

    file_contents=str(f1.read())
    list_of_records=file_contents.split('.')
    no_of_records=len(list_of_records)-1

    f1.seek(0)
    for i in range(no_of_records):
    print(pickle.load(f1))