{"id":324,"date":"2023-07-26T11:49:08","date_gmt":"2023-07-26T11:49:08","guid":{"rendered":"http:\/\/python.garden\/?p=324"},"modified":"2023-08-07T16:17:23","modified_gmt":"2023-08-07T16:17:23","slug":"getting-started-with-pythons-pickle","status":"publish","type":"post","link":"https:\/\/python.garden\/index.php\/2023\/07\/26\/getting-started-with-pythons-pickle\/","title":{"rendered":"Getting Started with Python&#8217;s Pickle"},"content":{"rendered":"\n<p>The pickle module in Python is used for serializing and de-serializing Python object structures, also known as marshaling or flattening. Serialization is the process of converting an object&#8217;s state to a byte stream, and the opposite operation, extracting the object from the byte stream, is called de-serialization.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Importing the module<\/strong>: Like any other Python module, before using <code>pickle<\/code>, you first need to import it.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pickle<\/pre><\/div>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Pickling<\/strong>: The process of serialization is called pickling. The <code>pickle.dump()<\/code> function is used to serialize an object hierarchy. It takes two arguments: the object you want to pickle and the file object in which you want to store the pickled object.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># create an example dictionary\ndata = {&quot;cat&quot;: &quot;meow&quot;, &quot;dog&quot;: &quot;bark&quot;}\n\n# open a file to write binary\nwith open(&quot;pets.pkl&quot;, &quot;wb&quot;) as file:\n    pickle.dump(data, file)<\/pre><\/div>\n\n\n\n<p>In the above code, we created a dictionary and pickled it into a file called &#8220;pets.pkl&#8221;. The &#8220;wb&#8221; mode stands for &#8220;write binary&#8221;.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Unpickling<\/strong>: The process of deserialization is called unpickling. The <code>pickle.load()<\/code> function is used to unpickle the object.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># open the file in read binary mode\nwith open(&quot;pets.pkl&quot;, &quot;rb&quot;) as file:\n    loaded_data = pickle.load(file)\nprint(loaded_data)<\/pre><\/div>\n\n\n\n<p>When you run the above code, it will display the original dictionary. The &#8220;rb&#8221; stands for &#8220;read binary&#8221;.<\/p>\n\n\n\n<ol class=\"wp-block-list\" start=\"4\">\n<li><strong>pickle.dumps() and pickle.loads()<\/strong>: These functions are used to pickle and unpickle data, but instead of writing or reading from a file, they simply return the pickled data as a bytes object or load from a bytes object.<\/li>\n<\/ol>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Python&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;python&quot;}\"># Pickling to a bytes object\npickled_data = pickle.dumps(data)\nprint(pickled_data)  # This will print a bytes object\n\n# Unpickling from a bytes object\nunpickled_data = pickle.loads(pickled_data)\nprint(unpickled_data)  # This will print the original dictionary<\/pre><\/div>\n\n\n\n<p>Some important points:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>pickle<\/code> module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.<\/li>\n\n\n\n<li>Not all Python data types can be pickled. Functions, classes, and methods cannot be pickled, so you cannot pickle the entire state of your Python program.<\/li>\n<\/ul>\n\n\n\n<p>Despite these limitations, <code>pickle<\/code> is very useful for storing and retrieving complex data in Python.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The pickle module in Python is used for serializing and de-serializing Python object structures, also known as marshaling or flattening. Serialization is the process of converting an object&#8217;s state to&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[214,213],"tags":[],"class_list":["post-324","post","type-post","status-publish","format-standard","hentry","category-beginners-guide","category-python"],"featured_image_src":null,"author_info":{"display_name":"shababdoo","author_link":"https:\/\/python.garden\/index.php\/author\/shababdoo\/"},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/324","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/comments?post=324"}],"version-history":[{"count":0,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/posts\/324\/revisions"}],"wp:attachment":[{"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/media?parent=324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/categories?post=324"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python.garden\/index.php\/wp-json\/wp\/v2\/tags?post=324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}