avatarYang Zhou

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2069

Abstract

iar with file handling in Python yet, no worries, there is a great tutorial talking about that:</i></p><div id="ed4c" class="link-block"> <a href="https://readmedium.com/file-handling-in-python-daee4586a64"> <div> <div> <h2>File Handling in Python</h2> <div><h3>Python is a very convenient tool to handle files. It will save you lots of time if you use it properly.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*hWC15XLtl79j2UlY)"></div> </div> </div> </a> </div><p id="6cb4">Another method called <code>dump</code> (no ending “s”) has a second parameter which receives a file name. Therefore, we can use <code>dump</code> to convert a object to bytes and save it to a file at once:</p> <figure id="2fdd"> <div> <div>

            <iframe class="gist-iframe" src="/gist/ZhouYang1993/525e5781d431920b4bda34b9423a4b66.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h2 id="ee3c">Unpickling by Loads or Load</h2><p id="0ca8">Unpickling, which is the opposite operation of pickling, refers to the process of converting bytes to Python objects. We can use <code>load</code> or <code>loads</code> method for this purpose:</p>
    <figure id="cc04">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/ZhouYang1993/f9a2da2fb22cf53f4d4a7bfe9df12f22.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h1 id="87ca">Use JSON to Replace Pickle</h1><p id="2676">There is a problem of the <code>pickle</code> module: it can only be used in Python. Other languages cannot under

Options

stand and convert the bytes generated by <code>pickle</code> module. If we want to transfer an object between different programming languages, we must serialize the object into a standard and language-independent format. A common way is to serialize it to JSON. JSON objects looks like strings which can be read directly in a website and be easily stored to disks or transmitted via network. For example, in Python web development, JSON is commonly used to transmit objects between Python at back-end and JavaScript at front-end.</p><p id="ac09">Python has a built-in module called <code>json</code> and its operations are similar to <code>pickle</code> module:</p> <figure id="b1ff"> <div> <div>

            <iframe class="gist-iframe" src="/gist/ZhouYang1993/b420114b1d7c883cf15e182d98d0c0fa.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><h1 id="9d10">Conclusion</h1><p id="ec27">Picking is the process of converting Python objects to bytes to make them convenient to be stored in disks or transmitted via network. If we only use Python in a project, the <code>pickle</code> module is useful. If we need to transmit data between different languages, the JSON is a common choice.</p><p id="117a"><b><i>Thanks for reading, more great Python tutorials is here:</i></b></p><div id="94b4" class="link-block">
      <a href="https://readmedium.com/the-ultimate-python-tutorial-keep-updating-13fa36cce44b">
        <div>
          <div>
            <h2>The Ultimate Python Tutorial (Keep Updating)</h2>
            <div><h3>The most valuable Python Tutorial you should save!</h3></div>
            <div><p>medium.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*rIHlzv0kwEQj9m-g)"></div>
          </div>
        </div>
      </a>
    </div></article></body>

Pickle in Python

Photo by chuttersnap on Unsplash

Introduction

When a program is running, all Python objects are temporarily stored in the system memory, and once the program ends, all occupied memory will be reclaimed by the operating system. Here comes a question: How can we store Python objects in disks or send them over a network? These are scenarios where the pickling is needed.

Pickling refers to the process of converting an Python object to bytes that can be conveniently stored on disks or sent over a network. It is called pickling in Python, and it is also called serialization, marshalling, flattening, etc. in other languages. These words all mean the same process and interchangeable.

Using the Pickle Module of Python

Python has a built-in module called pickle which makes the pickling process very simple and convenient.

There are two types of common used methods in this module: dumps or dump , and loads or load.

Pickling by Dumps or Dump

The dumps method is used to convert an object to bytes:

After the conversion of a Python object to bytes, we just need to save the bytes to a file on disks.

If you haven’t been familiar with file handling in Python yet, no worries, there is a great tutorial talking about that:

Another method called dump (no ending “s”) has a second parameter which receives a file name. Therefore, we can use dump to convert a object to bytes and save it to a file at once:

Unpickling by Loads or Load

Unpickling, which is the opposite operation of pickling, refers to the process of converting bytes to Python objects. We can use load or loads method for this purpose:

Use JSON to Replace Pickle

There is a problem of the pickle module: it can only be used in Python. Other languages cannot understand and convert the bytes generated by pickle module. If we want to transfer an object between different programming languages, we must serialize the object into a standard and language-independent format. A common way is to serialize it to JSON. JSON objects looks like strings which can be read directly in a website and be easily stored to disks or transmitted via network. For example, in Python web development, JSON is commonly used to transmit objects between Python at back-end and JavaScript at front-end.

Python has a built-in module called json and its operations are similar to pickle module:

Conclusion

Picking is the process of converting Python objects to bytes to make them convenient to be stored in disks or transmitted via network. If we only use Python in a project, the pickle module is useful. If we need to transmit data between different languages, the JSON is a common choice.

Thanks for reading, more great Python tutorials is here:

Programming
Python
Python3
Json
Python Programming
Recommended from ReadMedium