0

I have a python dictionary in a file in a format like this:

(dp0 S'Test' p1 S'Test1' p2 sS'Test2' p3 S'Test2' p4 sS'Starspy' p5 S'SHSN4N' p6 s.

see: Save a dictionary to a file (alternative to pickle) in Python?

and I want to read it back.

According to the question in the link, this has been created saving it with pickle. But, when I try to save a dictionary with pickle the format that I obtain does not correspond.

For example, the code:

import pickle
mydict = {'a': 1, 'b': 2, 'c': 3}
output = open('myfile.dict', 'wb')
pickle.dump(mydict, output)
output.close()

produces a file with the content

€}q (X aqKX bqKX cqKu.

I can read it back OK, but it has not the format of my file (that correspond to a nested dictionary). So, I have two questions:

First, how can I write a file with the format ... (dp0 S'Test' p1 S'Test1' p2 sS'Test2' p3 S'Test2' p4 sS'Starspy' p5 S'SHSN4N' p6 s. ?

Second, how can I read a file with that format?

Jose
  • 11
  • 1
  • 1

3 Answers3

2

If you want readable dictionaries then go with json it built into python and the output is pretty much like a dictionary.

tupui
  • 5,738
  • 3
  • 31
  • 52
  • I am sorry, your code does not work in my computer. It gives me an error. My problem is that I have a large file with a nested dictionary with many layers that someone pass to me (so I cannot save the data in a different format) and the file is in the format (dp0 S'Test' p1 S'Test1' p2 sS'Test2' p3 S'Test2' p4 sS'Starspy' p5 S'SHSN4N' p6 s. That is the reason I want to: 1) to learn how I can save a file with that kind of format and 2) mainly to be able to open the file I have. – Jose Oct 01 '17 at 08:10
1

The question is answered in the pickle module documentation: https://docs.python.org/2/library/pickle.html#data-stream-format

There are currently 3 different protocols which can be used for pickling.

Protocol version 0 is the original ASCII protocol and is backwards compatible with earlier versions of Python.

Protocol version 1 is the old binary format which is also compatible with earlier versions of Python.

Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of new-style classes.

Whatever you show in the beginning, it the protocol version 0, and is the default. In the end — the binary protocol versions 1 or 2.

Just specify the protocol version number:

>>> pickle.dump({'hello': 'world'}, file('f.txt', 'wt'))
>>> file('f.txt', 'rt').read()
"(dp0\nS'hello'\np1\nS'world'\np2\ns."

>>> pickle.dump({'hello': 'world'}, file('f.txt', 'wt'), 2)
>>> file('f.txt', 'rt').read()
'\x80\x02}q\x00U\x05helloq\x01U\x05worldq\x02s.'

>>> pickle.dump({'hello': 'world'}, file('f.txt', 'wt'), 1)
>>> file('f.txt', 'rt').read()
'}q\x00U\x05helloq\x01U\x05worldq\x02s.'

PS: Why not just use a readable format, e.g. json?

Sergey Vasilyev
  • 3,919
  • 3
  • 26
  • 37
0

The issue I have was a problem of versions. I was using version 3 and it works with version 2.

In that case, I can create a file with the format I have in the file I want to read, and I can now read it.

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
Jose
  • 11
  • 1
  • 1