Browse Source

JSON, Pickle

pull/135/merge
Jure Šorn 3 weeks ago
parent
commit
2f7033dbca
2 changed files with 10 additions and 10 deletions
  1. 8
      README.md
  2. 12
      index.html

8
README.md

@ -1782,9 +1782,9 @@ def read_json_file(filename):
### Write Collection to JSON File
```python
def write_to_json_file(filename, list_or_dict):
def write_to_json_file(filename, collection):
with open(filename, 'w', encoding='utf-8') as file:
json.dump(list_or_dict, file, ensure_ascii=False, indent=2)
json.dump(collection, file, ensure_ascii=False, indent=2)
```
@ -1798,14 +1798,14 @@ import pickle
<object> = pickle.loads(<bytes>) # Converts bytes object to object.
```
### Read Object from File
### Read Object from Pickle File
```python
def read_pickle_file(filename):
with open(filename, 'rb') as file:
return pickle.load(file)
```
### Write Object to File
### Write Object to Pickle File
```python
def write_to_pickle_file(filename, an_object):
with open(filename, 'wb') as file:

12
index.html

@ -55,7 +55,7 @@
<body>
<header>
<aside>February 6, 2025</aside>
<aside>February 7, 2025</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1481,9 +1481,9 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
<span class="hljs-keyword">return</span> json.load(file)
</code></pre></div>
<div><h3 id="writecollectiontojsonfile">Write Collection to JSON File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_json_file</span><span class="hljs-params">(filename, list_or_dict)</span>:</span>
<div><h3 id="writecollectiontojsonfile">Write Collection to JSON File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_json_file</span><span class="hljs-params">(filename, collection)</span>:</span>
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'w'</span>, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
json.dump(list_or_dict, file, ensure_ascii=<span class="hljs-keyword">False</span>, indent=<span class="hljs-number">2</span>)
json.dump(collection, file, ensure_ascii=<span class="hljs-keyword">False</span>, indent=<span class="hljs-number">2</span>)
</code></pre></div>
<div><h2 id="pickle"><a href="#pickle" name="pickle">#</a>Pickle</h2><p><strong>Binary file format for storing Python objects.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> pickle
@ -1492,12 +1492,12 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
</code></pre></div>
<div><h3 id="readobjectfromfile">Read Object from File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_pickle_file</span><span class="hljs-params">(filename)</span>:</span>
<div><h3 id="readobjectfrompicklefile">Read Object from Pickle File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_pickle_file</span><span class="hljs-params">(filename)</span>:</span>
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'rb'</span>) <span class="hljs-keyword">as</span> file:
<span class="hljs-keyword">return</span> pickle.load(file)
</code></pre></div>
<div><h3 id="writeobjecttofile">Write Object to File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_pickle_file</span><span class="hljs-params">(filename, an_object)</span>:</span>
<div><h3 id="writeobjecttopicklefile">Write Object to Pickle File</h3><pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_pickle_file</span><span class="hljs-params">(filename, an_object)</span>:</span>
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'wb'</span>) <span class="hljs-keyword">as</span> file:
pickle.dump(an_object, file)
</code></pre></div>
@ -2931,7 +2931,7 @@ $ deactivate <span class="hljs-comment"># Deactivates the active
<footer>
<aside>February 6, 2025</aside>
<aside>February 7, 2025</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Loading…
Cancel
Save