了解Python中的酸洗 [英] Understanding Pickling in Python

查看:110
本文介绍了了解Python中的酸洗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近接到一个作业,需要以腌制形式放置字典(每个键都指向一个列表).唯一的问题是我不知道什么是腌制形式.有人能指出我一些正确资源的正确方向,以帮助我学习这一概念吗?

I have recently got an assignment where I need to put a dictionary (where each key refers to a list) in pickled form. The only problem is I have no idea what pickled form is. Could anyone point me in the right direction of some good resources to help me learn this concept?

推荐答案

pickle模块实现了一个基本但功能强大的算法,用于对Python对象结构进行序列化和反序列化.

The pickle module implements a fundamental, but powerful algorithm for serializing and de-serializing a Python object structure.

Pickling-是将Python对象层次结构转换为字节流的过程,而 Unpickling-是相反的操作,由此将字节流转换回为字节流对象层次结构.

Pickling - is the process whereby a Python object hierarchy is converted into a byte stream, and Unpickling - is the inverse operation, whereby a byte stream is converted back into an object hierarchy.

酸洗(和解酸)也称为序列化编组展平.

Pickling (and unpickling) is alternatively known as serialization, marshalling, or flattening.

import pickle

data1 = {'a': [1, 2.0, 3, 4+6j],
         'b': ('string', u'Unicode string'),
         'c': None}

selfref_list = [1, 2, 3]
selfref_list.append(selfref_list)

output = open('data.pkl', 'wb')

# Pickle dictionary using protocol 0.
pickle.dump(data1, output)

# Pickle the list using the highest protocol available.
pickle.dump(selfref_list, output, -1)

output.close()

要读取已腌制的文件-

import pprint, pickle

pkl_file = open('data.pkl', 'rb')

data1 = pickle.load(pkl_file)
pprint.pprint(data1)

data2 = pickle.load(pkl_file)
pprint.pprint(data2)

pkl_file.close()

源- https://docs.python.org/2/library/pickle. html

这篇关于了解Python中的酸洗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆