|
5 | 5 | <link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
|
6 | 6 | <link href="https://learnbyexample.github.io"/>
|
7 | 7 | <generator uri="https://www.getzola.org/">Zola</generator>
|
8 |
| - <updated>2022-11-29T00:00:00+00:00</updated> |
| 8 | + <updated>2022-12-07T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>Python tip 20: saving and loading json</title> |
| 12 | + <published>2022-12-07T00:00:00+00:00</published> |
| 13 | + <updated>2022-12-07T00:00:00+00:00</updated> |
| 14 | + <link rel="alternate" href="https://learnbyexample.github.io/tips/python-tip-20/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/python-tip-20/</id> |
| 16 | + <content type="html"><p>JSON (JavaScript Object Notation) is one of the ways you can store and retrieve data necessary for functioning of an application. For example, my projects <a href="https://github.com/learnbyexample/py_regular_expressions/tree/master/interactive_exercises">Python regex exercises</a> and <a href="https://github.com/learnbyexample/TUI-apps/blob/main/CLI-Exercises">Linux CLI text processing exercises</a> need to load questions and save user progress. You might wonder why not just a plain text file? I needed <code>dict</code> in the code anyway and JSON offered seamless transition. Also, this arrangement avoided having to write extra code and test it for potential parsing issues.</p> |
| 17 | +<p>The <code>json</code> builtin module is handy for such purposes. Here's an example of saving a <code>dict</code> object:</p> |
| 18 | +<pre data-lang="python" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-python "><code class="language-python" data-lang="python"><span style="color:#72ab00;">&gt;&gt;&gt; import </span><span>json |
| 19 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span>marks </span><span style="color:#72ab00;">= </span><span>{</span><span style="color:#d07711;">&#39;Rahul&#39;</span><span>: </span><span style="color:#b3933a;">86</span><span>, </span><span style="color:#d07711;">&#39;Ravi&#39;</span><span>: </span><span style="color:#b3933a;">92</span><span>, </span><span style="color:#d07711;">&#39;Rohit&#39;</span><span>: </span><span style="color:#b3933a;">75</span><span>, </span><span style="color:#d07711;">&#39;Rajan&#39;</span><span>: </span><span style="color:#b3933a;">79</span><span>} |
| 20 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; with </span><span style="color:#b39f04;">open</span><span>(</span><span style="color:#d07711;">&#39;marks.json&#39;</span><span>, </span><span style="color:#d07711;">&#39;w&#39;</span><span>) </span><span style="color:#72ab00;">as </span><span>f: |
| 21 | +</span><span style="color:#b3933a;">... </span><span>json.</span><span style="color:#5597d6;">dump</span><span>(marks, f, </span><span style="color:#5597d6;">indent</span><span style="color:#72ab00;">=</span><span style="color:#b3933a;">4</span><span>) |
| 22 | +</span><span style="color:#b3933a;">... |
| 23 | +</span></code></pre> |
| 24 | +<p>In the above example, <code>indent</code> is used for pretty printing. Here's how the file looks like:</p> |
| 25 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ cat marks.json |
| 26 | +</span><span>{ |
| 27 | +</span><span> </span><span style="color:#d07711;">&quot;Rahul&quot;</span><span style="color:#72ab00;">: </span><span style="color:#b3933a;">86</span><span>, |
| 28 | +</span><span> </span><span style="color:#d07711;">&quot;Ravi&quot;</span><span style="color:#72ab00;">: </span><span style="color:#b3933a;">92</span><span>, |
| 29 | +</span><span> </span><span style="color:#d07711;">&quot;Rohit&quot;</span><span style="color:#72ab00;">: </span><span style="color:#b3933a;">75</span><span>, |
| 30 | +</span><span> </span><span style="color:#d07711;">&quot;Rajan&quot;</span><span style="color:#72ab00;">: </span><span style="color:#b3933a;">79 |
| 31 | +</span><span>} |
| 32 | +</span></code></pre> |
| 33 | +<p>And here's an example of loading a JSON file:</p> |
| 34 | +<pre data-lang="python" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-python "><code class="language-python" data-lang="python"><span style="color:#72ab00;">&gt;&gt;&gt; with </span><span style="color:#b39f04;">open</span><span>(</span><span style="color:#d07711;">&#39;marks.json&#39;</span><span>) </span><span style="color:#72ab00;">as </span><span>f: |
| 35 | +</span><span style="color:#b3933a;">... </span><span>marks </span><span style="color:#72ab00;">= </span><span>json.</span><span style="color:#5597d6;">load</span><span>(f) |
| 36 | +</span><span style="color:#b3933a;">... |
| 37 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span>marks |
| 38 | +</span><span>{</span><span style="color:#d07711;">&#39;Rahul&#39;</span><span>: </span><span style="color:#b3933a;">86</span><span>, </span><span style="color:#d07711;">&#39;Ravi&#39;</span><span>: </span><span style="color:#b3933a;">92</span><span>, </span><span style="color:#d07711;">&#39;Rohit&#39;</span><span>: </span><span style="color:#b3933a;">75</span><span>, </span><span style="color:#d07711;">&#39;Rajan&#39;</span><span>: </span><span style="color:#b3933a;">79</span><span>} |
| 39 | +</span></code></pre> |
| 40 | +<p><img src="/images/info.svg" alt="info" /> See <a href="https://docs.python.org/3/library/json.html">docs.python: json</a> for documentation, more examples, other methods, caveats and so on.</p> |
| 41 | +<p><strong>Video demo</strong>:</p> |
| 42 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/Fd28UTqcU3k" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 43 | +<br> |
| 44 | +<p><img src="/images/info.svg" alt="info" /> See also my <a href="https://github.com/learnbyexample/100_page_python_intro">100 Page Python Intro</a> ebook.</p> |
| 45 | +</content> |
| 46 | + </entry> |
10 | 47 | <entry xml:lang="en">
|
11 | 48 | <title>Vim tip 18: moving within long lines</title>
|
12 | 49 | <published>2022-11-29T00:00:00+00:00</published>
|
|
0 commit comments