H
H
hokim2014-01-13 15:10:25
Python
hokim, 2014-01-13 15:10:25

Working with pickle. How to output all dictionary data from a file?

How to output all dictionary data from a file? The code:

import pickle
f = open('users.txt', 'rb')

e = pickle.load(f)

pickle.load(f) outputs only one line. Tried like:
import pickle
f = open('users.txt', 'rb')
e = pickle.load(f)

while e.keys() != '':
    print(pickle.load(f))

Here the program outputs all lines except the last one. Instead of the last error EOFError
What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
leclecovich, 2014-01-13
@rhino_o

Try like this:

from pickle import dumps, load


def store(arg):
    f = open('file.txt', 'w')
    f.write(dumps(arg))
    f.close()


def extract():
    f = open('file.txt', 'r')
    result = load(f)
    f.close()
    return result


d0 = {'one': 1, 'two': 2}
store(d0)
d1 = extract()
print d1

It also shows the difference between dump/load and dumps/loads. By the way, Python 2.7.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question