简体   繁体   English

以可读格式在python中腌制字典

[英]pickle a dictonary in python in a readable format

I have a python dictionary in a file in a format like this:我在一个文件中有一个 python 字典,格式如下:

(dp0 S'Test' p1 S'Test1' p2 sS'Test2' p3 S'Test2' p4 sS'Starspy' p5 S'SHSN4N' p6 s. (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?请参阅: 在 Python 中将字典保存到文件(替代 pickle)?

and I want to read it back.我想把它读回来。

According to the question in the link, this has been created saving it with pickle.根据链接中的问题,这是用pickle保存的。 But, when I try to save a dictionary with pickle the format that I obtain does not correspond.但是,当我尝试用 pickle 保存字典时,我获得的格式不对应。

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. €}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. ?首先,我怎样才能写一个格式为 ... (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该问题在pickle模块文档中得到了回答: https : //docs.python.org/2/library/pickle.html#data-stream-format

There are currently 3 different protocols which can be used for pickling.目前有 3 种不同的协议可用于酸洗。

Protocol version 0 is the original ASCII protocol and is backwards compatible with earlier versions of Python.协议版本 0 是原始 ASCII 协议,向后兼容早期版本的 Python。

Protocol version 1 is the old binary format which is also compatible with earlier versions of Python.协议版本 1 是旧的二进制格式,它也与早期版本的 Python 兼容。

Protocol version 2 was introduced in Python 2.3.协议版本 2 是在 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.无论您在开始时显示什么,它都是协议版本 0,并且是默认值。 In the end — the binary protocol versions 1 or 2.最后——二进制协议版本 1 或 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? PS:为什么不使用可读格式,例如 json?

The issue I have was a problem of versions.我遇到的问题是版本问题。 I was using version 3 and it works with version 2.我使用的是版本 3,它适用于版本 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.在这种情况下,我可以使用我想要读取的文件中的格式创建一个文件,现在我可以读取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM