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?
如果你想要可读的字典,那么使用json它内置在 python 中,输出非常像字典。
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, eg json?
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.