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>