简体   繁体   English

将字典保存到文件Python

[英]Save a dictionary to a file Python

Say I got a dictionary like this: 说我有这样的字典:

Test = {"apple":[3,{1:1,3:5,6:7}],"banana":[4,{1:1,3:5,6:7,11:2}]}

Now I want to save this dictionary into a temporary file so that I can reconstruct the dictionary later. 现在我想将这个字典保存到一个临时文件中,以便我以后可以重新构建字典。 (I am doing external sorting XD) (我正在做外部排序XD)

Can any kind guy help me? 任何善良的人都可以帮助我吗? I know there is a way to save it in csv format, but this one is a special kind of dictionary. 我知道有一种方法可以用csv格式保存它,但这是一种特殊的字典。 Thx a lot. 多谢。

In order to save a data structure to a file you need to decide on a serialization format. 为了将数据结构保存到文件,您需要决定序列化格式。 A serialization format takes an in-memory data structure and turns it into a sequence of bytes that can be written to a file. 序列化格式采用内存数据结构并将其转换为可写入文件的字节序列。 The process of turning that sequence of bytes back into a data structure is called deserialization . 将该字节序列转换回数据结构的过程称为反序列化

Python provides a number of options for seralization with different characteristics. Python为具有不同特征的seralization提供了许多选项。 Here are two common choices and their tradeoffs: 以下是两种常见的选择和权衡:

  • pickle uses a compact format that can represent almost any Python object. pickle使用紧凑的格式,几乎可以代表任何Python对象。 However, it's specific to Python, so only Python programs can (easily) decode the file later. 但是,它特定于Python,因此只有Python程序可以(轻松地)稍后解码该文件。 The details of the format can also vary between Python releases, so it's best to use pickle only if you will re-read the data file with the same or a newer version of Python than the one that created it. 格式的细节也可能因Python版本而异,因此,只有在使用与创建它相同或更新版本的Python重新读取数据文件时,才最好使用pickle。 Pickle is able to deal with recursive data structures. Pickle能够处理递归数据结构。 Finally, pickle is inappropriate for reading data provided by other possibly-malicious programs, since decoding a pickled data structure can cause arbitrary code to run in your application in order to reconstruct complex objects. 最后,pickle不适合读取其他可能的恶意程序提供的数据,因为解码pickle数据结构会导致任意代码在您的应用程序中运行,以便重建复杂的对象。

  • json uses a human-readable text-based format that can represent only a small set of data types: numbers, strings, None , booleans, lists and dictionaries. json使用人类可读的基于文本的格式,它只能表示一小组数据类型:数字,字符串, None ,布尔值,列表和字典。 However, it is a standard format that can be read by equivalent libraries in many other programming languages, and it does not vary from one Python release to another. 但是,它是一种标准格式,可以被许多其他编程语言中的等效库读取,并且从一个Python版本到另一个版本不同。 JSON does not support recursive data structures, but it is generally safe to decode a JSON object from an unknown source except that of course the resulting data structure might use a lot of memory. JSON不支持递归数据结构,但从未知来源解码JSON对象通常是安全的,当然结果数据结构可能会占用大量内存。

In both of these modules the dump function can write a data structure to a file and the load function can later recover it. 在这两个模块中, dump功能可以将数据结构写入文件, load功能可以在以后恢复它。 The difference is in the format of the data written to the file. 不同之处在于写入文件的数据格式。

The pickle module is quite convenient for serializing python data. pickle模块非常便于序列化python数据。 It is also probably the fastest way to dump and reload a python data structure. 它也可能是转储和重新加载python数据结构的最快方法。

>>> import pickle
>>> Test = {"apple":[3,{1:1,3:5,6:7}],"banana":[4,{1:1,3:5,6:7,11:2}]}
>>> pickle.dump(Test, open('test_file', 'w'))
>>> pickle.load(open('test_file', 'r'))
{'apple': [3, {1: 1, 3: 5, 6: 7}], 'banana': [4, {1: 1, 3: 5, 11: 2, 6: 7}]}

For me, clearly, the best serializer is msgpack . 对我来说,最好的序列化程序是msgpack

http://jmoiron.net/blog/python-serialization/ http://jmoiron.net/blog/python-serialization/

Same methods as the others. 与其他方法相同。

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

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