python objects and pickle

#python object

1. What is a python object

2. Detailed pickle pickle

 

1. What is a python object

Python's built-in object types mainly include numbers, strings, lists, tuples, dictionaries, sets, and more. In fact, in python, everything is an object.

 

2. Detailed pickle pickle

In python, if we have some objects that need to be stored persistently without losing the type and data of our object, we need to serialize these objects. After serialization, when we need to use it, we restore the original data. . This process of serialization, we call it pickle.

 

#pickle pickled

Import pickle

#dumps (object ) serialize the object

lista= [“mingyue ”,”jishi ”,”you ”]

listb=pickle.dumps(lista)

print listb

 

#loads( string) restores the object as it is, and the object type is also restored to the original format

listc=pickle.loads(listb)

print listc

 

#dump (object , file ) stores the object in a file and serializes it

group1= (“bajiu ”,”wen ”,”qingtian ”)

f1=file(‘1.pk1’,’wb’)

pickle.dump(group1,f1,Ture)

f1.close()

 

#load (object , file ) restores the data stored in the file by dump ()

f2=file(‘1.pk1’,’rb’)

t=pickle.load(f2)

print t

f2.close()

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324439725&siteId=291194637