Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)
I have as of late got an assignment where I want to put a dictionary in pickled structure. The issue is I have no clue about what pickled structure is. Would anyone be able to point me to the correct way of some great resources to assist me with learning this idea?

1 Answer

0 votes
by (26.4k points)

The pickle module executes a major, yet incredible algorithm for serializing and de-serializing a Python object structure. 

Pickling - is the cycle whereby a Python object hierarchy is changed over into a byte stream, and Unpickling - is the converse activity, whereby a byte stream is changed over once again into an object hierarchy. 

Pickling (and unpickling) is on the other hand known as serialization, marshaling, or even 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()

If you want to read from a pickled file, then:

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()

Click on this link, for source

Wanna become a python expert? Come and join the python certification course and get certified.

Related questions

0 votes
1 answer
asked Mar 1, 2021 in Python by Rekha (2.2k points)
0 votes
1 answer
0 votes
1 answer
0 votes
4 answers
asked Apr 4, 2021 in Python by laddulakshana (16.4k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...