{"id":8453,"date":"2023-11-08T18:34:50","date_gmt":"2023-11-08T13:04:50","guid":{"rendered":"https:\/\/python-programs.com\/?p=8453"},"modified":"2023-11-10T12:23:20","modified_gmt":"2023-11-10T06:53:20","slug":"python-data-persistence-pickle-module","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/","title":{"rendered":"Python Data Persistence – pickle Module"},"content":{"rendered":"

Python Data Persistence – pickle Module<\/h2>\n

The serialization format used by the pickle module, which is a part of Python\u2019s built-in module library, is very Python-specific. While this fact can work as an advantage that it doesn\u2019t face any restrictions by certain external standards such as JSON format, the major disadvantage is that non-Python applications may not be able to reconstruct \u2018pickled\u2019 objects. Also, the pickle module is not considered secure when it comes to unpickling data received from an unauthenticated or untrusted source.<\/p>\n

The pickle module defines the module-level dumps ( ) function to obtain a byte string \u2018pickled\u2019 representation of any Python object. Its counterpart function loads () reconstructs (\u2018unpickles\u2019) the byte string identical Python object.<\/p>\n

Following code snippet demonstrates the use of dumps () and loads () functions:<\/p>\n

Example<\/strong><\/p>\n

>>> import pickle\r\n>>> numbers=[10,20,30,40]\r\n>>> pickledata=pickle.dumps(numbers)\r\n>>> pickledata\r\nb'\\x80\\x03]q\\x00(K\\nK\\xl4K\\xleK(e.'\r\n>>> #unpickled data\r\n. . .\r\n>>> unpickledata=pickle.loads(pickledata)\r\n>>> unpickledata\r\n[10, 20, 30, 40]\r\n>>><\/pre>\n

There are dump ( ) and load () functions that respectively write pickled data persistently to a file-like object (which may be a disk file, a memory buffer object, or a network socket object) having binary and write \u2018wb\u2019 mode enabled, and reconstruct identical object from a file-like object having \u2018rb\u2019 permission.<\/p>\n

Example<\/strong><\/p>\n

>>> #pickle to file\r\n. . .\r\n>>> import pickle\r\n>>> numbers=[ 10 , 20 , 30 , 40 ]\r\n>>> file=open ('numbers .dat' , ' wb ')\r\n>>> pickle . dump (numbers, file)\r\n>>> file . close ()\r\n>>> #unpickle from file\r\n. . .\r\n>>> file=open {1 numbers . dat' , ' rb' )\r\n>>> unpickledata=pickle . load (file)\r\n>>> unpickledata\r\n[10, 20, 30, 40]\r\n>>><\/pre>\n

Almost any type of Python object can be pickled. This includes built-in types, built-in, and user-defined functions, and objects of user-defined classes.<\/p>\n

The pickle module also provides object-oriented API as a substitute for module-level dumps () \/loads () and dump () \/load () functions. The module has a pickier class whose object can invoke dump () or dumps () method to \u2018pickle\u2019 an object. Conversely, the unpicker class defines load () and loads () methods.<\/p>\n

Following script has a person class whose objects are pickled in a file using pickier class. Original objects are obtained by the load () method of unpicker class.<\/p>\n

Example<\/strong><\/p>\n

from pickle import Pickier, Unpickler\r\nclass User:\r\ndef__init__(self,name, email, pw):\r\nself.name=name\r\nself.email=email\r\nself.pw=pw\r\ndef__str__(self):\r\nreturn ('Name: { } email: { } password: {}'. \\ format(self.name, self.email, self.pw))\r\nuser1=User('Raj an', 'rl23@gmail.com', 'rajanl23')\r\nuser2=User('Sudheer', 's.ll@gmail.com', 's 11')\r\nprint ('before pickling..')\r\nprint (user1)\r\nprint (user2)\r\nfile=open (' users . dat' , ' wb' )\r\nPickier (file) .dump (userl)\r\nPickier (file) .dump(user2)\r\nfile.close ()\r\nfile=open ( ' users . dat' , ' rb ' )\r\nobj 1=Unpickler (file) . load ()\r\nprint ('unpickled objects')\r\nprint (obj1)\r\nobj2=Unpickler (file) . load()\r\nprint (obj2)<\/pre>\n

Output:<\/strong><\/p>\n

E:\\python37>python pick1-example.py before pickling.\r\nName: Rajan email: rl23w-gmail.com password: rajanl23 \r\nName: Sudheer email: s.lKwgmail.com password: s_ll unpickled objects\r\nName: Rajan email: rl2 3wgmail.com password: rajanl23 \r\nName: Sudheer email: s.llwgmail.com password: s_ll E:\\python3 7 ><\/pre>\n","protected":false},"excerpt":{"rendered":"

Python Data Persistence – pickle Module The serialization format used by the pickle module, which is a part of Python\u2019s built-in module library, is very Python-specific. While this fact can work as an advantage that it doesn\u2019t face any restrictions by certain external standards such as JSON format, the major disadvantage is that non-Python applications …<\/p>\n

Python Data Persistence – pickle Module<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Data Persistence - pickle Module - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Data Persistence - pickle Module - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Python Data Persistence – pickle Module The serialization format used by the pickle module, which is a part of Python\u2019s built-in module library, is very Python-specific. While this fact can work as an advantage that it doesn\u2019t face any restrictions by certain external standards such as JSON format, the major disadvantage is that non-Python applications … Python Data Persistence – pickle Module Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-11-08T13:04:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:53:20+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prasanna\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/\",\"name\":\"Python Data Persistence - pickle Module - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-11-08T13:04:50+00:00\",\"dateModified\":\"2023-11-10T06:53:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Data Persistence – pickle Module\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Data Persistence – pickle Module\",\"datePublished\":\"2023-11-08T13:04:50+00:00\",\"dateModified\":\"2023-11-10T06:53:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#webpage\"},\"wordCount\":281,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\",\"name\":\"Prasanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"caption\":\"Prasanna\"},\"url\":\"https:\/\/python-programs.com\/author\/prasanna\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Data Persistence - pickle Module - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/","og_locale":"en_US","og_type":"article","og_title":"Python Data Persistence - pickle Module - Python Programs","og_description":"Python Data Persistence – pickle Module The serialization format used by the pickle module, which is a part of Python\u2019s built-in module library, is very Python-specific. While this fact can work as an advantage that it doesn\u2019t face any restrictions by certain external standards such as JSON format, the major disadvantage is that non-Python applications … Python Data Persistence – pickle Module Read More »","og_url":"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-11-08T13:04:50+00:00","article_modified_time":"2023-11-10T06:53:20+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#webpage","url":"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/","name":"Python Data Persistence - pickle Module - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-11-08T13:04:50+00:00","dateModified":"2023-11-10T06:53:20+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-data-persistence-pickle-module\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Data Persistence – pickle Module"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Data Persistence – pickle Module","datePublished":"2023-11-08T13:04:50+00:00","dateModified":"2023-11-10T06:53:20+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#webpage"},"wordCount":281,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-data-persistence-pickle-module\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb","name":"Prasanna","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","caption":"Prasanna"},"url":"https:\/\/python-programs.com\/author\/prasanna\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8453"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=8453"}],"version-history":[{"count":2,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8453\/revisions"}],"predecessor-version":[{"id":9378,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/8453\/revisions\/9378"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=8453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=8453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=8453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}