by
2
7
645k
11
Top 1% !
Popular
Famous
Tagged
Easy-to-find
Specified
OpenSource
Popularity: 492nd place
Created
Modified Oct 24, 2013
Architecturex86_64
LanguagePython
LicenseMIT_X11

Using Pickle to Save Objects in Python - String Serialization

You can use the pickle module to save data in a serialized format, typically in a txt string, performing what is better known as data serialization, marshalling, or flattening. 

Pickle allows you to save an object to a file for later retrieval. 

Pickling some data will write the python object (dictionary, list, tuple, class) as one long stream of bytes, usually in a not human-readable format, to a specific text file.
post this code
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/11808/Using-Pickle-to-Save-Objects-in-Python--?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
import pickle # The data to save myDict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} # The object to pickle object_pi = myDict # The file where to write the serialized string file_pi = open('filename_pi.obj', 'w') # Dump the data as string to file pickle.dump(object_pi, file_pi) file_pi.close()

Other Steps and Solutions

If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus