Skip to content

Commit

Permalink
JSON, Pickle
Browse files Browse the repository at this point in the history
  • Loading branch information
gto76 committed Feb 7, 2025
1 parent 4ac35a7 commit 2f7033d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```


Expand All @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -1481,9 +1481,9 @@
<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
Expand All @@ -1492,12 +1492,12 @@
</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>
Expand Down Expand Up @@ -2931,7 +2931,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment


<footer>
<aside>February 6, 2025</aside>
<aside>February 7, 2025</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

Expand Down

0 comments on commit 2f7033d

Please sign in to comment.