There are also other programming languages than Python.
Other languages can not work with python code. If you would like to "talk" with such programs - pass them some processing information or to get results from them - you have to pass the information in a simplified form.
Most programming languages have some numbers, some sort of lists,
a variety of strings and some variation of dictionaries
(or several ways to create dictionaries).
And they have a way how to write True
, False
and None
.
These basic types are usually sufficient for information handover
in a legible form, although there are not the exact equivalents in all languages
(Python has two basic types of numbers - int
andfloat
).
So we will focus on them.
Another problem is data transfer: so for you to be able to write data on disk or transfer via the Internet, it has to be converted to a sequence of bytes (numbers from 0 to 255). Simplified: you have to convert it to a string.
There are plenty of ways to encode data into text. Each way is trying to find the right balance between legibility for people/computers, length of record, security, options and extensibility. We already know the syntax for Python:
{
'name': 'Anna',
'city': 'Prague',
'languages': ['Czech', 'English', 'Python'],
'age': 26,
}
Another way to write data is [YAML] (http://www.yaml.org/):
name: Anna
city: Prague
languages:
- Czech
- English
- Python
age: 26
Or maybe [Bencode] (http://en.wikipedia.org/wiki/Bencode):
d6: language9: czech11: english6: Pythone4: agei26e6: city4: Prague6: name4: Annae
There are also non-text formats like [Pickle 3] (https://docs.python.org/3/library/pickle.html):
}q(XjménoqXAnnaqXmÄtoqXBrnoqXjazykyq]q(X ÄeÅ¡tinaqX
angliÄtinaXPythonq eXvÄq
K▒u.
Finally, there is also [JSON] (http://json.org/) (Javascript Object Notation), which, for its simplicity, has expanded the most:
{
"Name": "Anna",
"City": "Prague",
"Languages": ["Czech", "English", "Python"],
"Age": 26
}
Keep in mind that although JSON looks similar to code in Python, it's another format with its own rules. Do not confuse them!
At first I do not recommend writing JSON manually; let computer decide where to write commas and quotation marks.
Object encoding in JSON is simple: there is a json
module,
whose load
method retrieves data from the string:
import json
json_string = """
{
"name": "Anna",
"city": "Brno",
"languages": ["Czech", "English", "Python"],
"age": 26
}
"""
data = json.loads(json_string)
print(data)
print(data['city'])
And then there is the dumps
method, which decodes the given data
and returns a string.
The string that dumps(data)
returns is suitable for computer
treatment.
If you want to read it, it is better to set ensure_ascii = False
(so that accented letters are not encoded with\
)
and indent = 2
(indent with two spaces).
>>> print(json.dumps(data, ensure_ascii = False, indent = 2))
{
"name": "Anna",
"city": "Brno",
"languages": [
"Czech",
"English",
"Python"
],
"age": 26
}
A complete description of json
module -
including write/read functions directly to/from files -
is in the documentation.
{ "data": { "sessionMaterial": { "id": "session-material:2018/pyladies-en-prague:dict:1", "title": "JSON", "html": "\n \n \n\n <h1>JSON</h1>\n<p>There are also other programming languages ​​than Python.</p>\n<p>Other languages ​​can not work with python code.\nIf you would like to "talk" with such programs -\npass them some processing information\nor to get results from them -\nyou have to pass the information in a simplified form.</p>\n<h2>Types</h2>\n<p>Most programming languages ​​have some numbers, some sort of lists,\na variety of strings and some variation of dictionaries\n(or several ways to create dictionaries).\nAnd they have a way how to write <code>True</code>, <code>False</code> and <code>None</code>.</p>\n<p>These basic types are usually sufficient for information handover\nin a legible form, although there are not the exact equivalents in all languages\n(Python has two basic types of numbers - <code>int</code> and<code>float</code>).\nSo we will focus on them.</p>\n<h2>Data encoding</h2>\n<p>Another problem is data transfer:\nso for you to be able to write data on disk or transfer\nvia the Internet, it has to be converted to a sequence of <em>bytes</em> (numbers from 0 to 255).\nSimplified: you have to convert it to a string.</p>\n<p>There are plenty of ways to encode data into text.\nEach way is trying to find the right balance between\nlegibility for people/computers, length of record,\nsecurity, options and extensibility.\nWe already know the syntax for Python:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"p\">{</span>\n <span class=\"s1\">'name'</span><span class=\"p\">:</span> <span class=\"s1\">'Anna'</span><span class=\"p\">,</span>\n <span class=\"s1\">'city'</span><span class=\"p\">:</span> <span class=\"s1\">'Prague'</span><span class=\"p\">,</span>\n <span class=\"s1\">'languages'</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s1\">'Czech'</span><span class=\"p\">,</span> <span class=\"s1\">'English'</span><span class=\"p\">,</span> <span class=\"s1\">'Python'</span><span class=\"p\">],</span>\n <span class=\"s1\">'age'</span><span class=\"p\">:</span> <span class=\"mi\">26</span><span class=\"p\">,</span>\n<span class=\"p\">}</span>\n</pre></div><p>Another way to write data is [YAML] (<a href=\"http://www.yaml.org/\">http://www.yaml.org/</a>):</p>\n<div class=\"highlight\"><pre><span></span><span class=\"nt\">name</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">Anna</span>\n<span class=\"nt\">city</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">Prague</span>\n<span class=\"nt\">languages</span><span class=\"p\">:</span>\n<span class=\"err\">  </span> <span class=\"p p-Indicator\">-</span> <span class=\"l l-Scalar l-Scalar-Plain\">Czech</span>\n<span class=\"err\">  </span> <span class=\"p p-Indicator\">-</span> <span class=\"l l-Scalar l-Scalar-Plain\">English</span>\n<span class=\"err\">  </span> <span class=\"p p-Indicator\">-</span> <span class=\"l l-Scalar l-Scalar-Plain\">Python</span>\n<span class=\"nt\">age</span><span class=\"p\">:</span> <span class=\"l l-Scalar l-Scalar-Plain\">26</span>\n</pre></div><p>Or maybe [Bencode] (<a href=\"http://en.wikipedia.org/wiki/Bencode\">http://en.wikipedia.org/wiki/Bencode</a>):</p>\n<div class=\"highlight\"><pre><code>d6: language9: czech11: english6: Pythone4: agei26e6: city4: Prague6: name4: Annae</code></pre></div><p>There are also non-text formats like\n[Pickle 3] (<a href=\"https://docs.python.org/3/library/pickle.html\">https://docs.python.org/3/library/pickle.html</a>):</p>\n<div class=\"highlight\"><pre><code>}q(XjménoqXAnnaqXmÄtoqXBrnoqXjazykyq]q(X ÄeÅ¡tinaqX\n angliÄtinaXPythonq eXvÄq\nK▒u.</code></pre></div><p>Finally, there is also [JSON] (<a href=\"http://json.org/\">http://json.org/</a>)\n(<em>Javascript Object Notation</em>),\nwhich, for its simplicity, has expanded the most:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"p\">{</span>\n  <span class=\"nt\">"Name"</span><span class=\"p\">:</span> <span class=\"s2\">"Anna"</span><span class=\"p\">,</span>\n  <span class=\"nt\">"City"</span><span class=\"p\">:</span> <span class=\"s2\">"Prague"</span><span class=\"p\">,</span>\n  <span class=\"nt\">"Languages"</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">"Czech"</span><span class=\"p\">,</span> <span class=\"s2\">"English"</span><span class=\"p\">,</span> <span class=\"s2\">"Python"</span><span class=\"p\">],</span>\n  <span class=\"nt\">"Age"</span><span class=\"p\">:</span> <span class=\"mi\">26</span>\n<span class=\"p\">}</span>\n</pre></div><div class=\"admonition note\"><p>Keep in mind that although JSON looks similar to code\nin Python, it's another format with its own rules.\nDo not confuse them!</p>\n<p>At first I do not recommend writing JSON manually;\nlet computer decide where to write\ncommas and quotation marks.</p>\n</div><h2>JSON in Python</h2>\n<p>Object encoding in JSON is simple: there is a <code>json</code> module,\nwhose <code>load</code> method retrieves data from the string:</p>\n<div class=\"highlight\"><pre><span></span><span class=\"kn\">import</span> <span class=\"nn\">json</span>\n\n<span class=\"n\">json_string</span> <span class=\"o\">=</span> <span class=\"s2\">"""</span>\n<span class=\"s2\"> {</span>\n<span class=\"s2\"> "name": "Anna",</span>\n<span class=\"s2\"> "city": "Brno",</span>\n<span class=\"s2\"> "languages": ["Czech", "English", "Python"],</span>\n<span class=\"s2\"> "age": 26</span>\n<span class=\"s2\"> }</span>\n<span class=\"s2\">"""</span>\n\n<span class=\"n\">data</span> <span class=\"o\">=</span> <span class=\"n\">json</span><span class=\"o\">.</span><span class=\"n\">loads</span><span class=\"p\">(</span><span class=\"n\">json_string</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">)</span>\n<span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">[</span><span class=\"s1\">'city'</span><span class=\"p\">])</span>\n</pre></div><p>And then there is the <code>dumps</code> method, which decodes the given data\nand returns a string.</p>\n<p>The string that <code>dumps(data)</code> returns is suitable for computer\ntreatment.\nIf you want to read it, it is better to set <code>ensure_ascii = False</code> \n(so that accented letters are not encoded with<code>\\</code>)\nand <code>indent = 2</code> (indent with two spaces).</p>\n<div class=\"highlight\"><pre><span></span><span class=\"gp\">>>> </span><span class=\"k\">print</span><span class=\"p\">(</span><span class=\"n\">json</span><span class=\"o\">.</span><span class=\"n\">dumps</span><span class=\"p\">(</span><span class=\"n\">data</span><span class=\"p\">,</span> <span class=\"n\">ensure_ascii</span> <span class=\"o\">=</span> <span class=\"bp\">False</span><span class=\"p\">,</span> <span class=\"n\">indent</span> <span class=\"o\">=</span> <span class=\"mi\">2</span><span class=\"p\">))</span>\n<span class=\"go\">{</span>\n<span class=\"go\"> "name": "Anna",</span>\n<span class=\"go\"> "city": "Brno",</span>\n<span class=\"go\"> "languages": [</span>\n<span class=\"go\"> "Czech",</span>\n<span class=\"go\"> "English",</span>\n<span class=\"go\"> "Python"</span>\n<span class=\"go\"> ],</span>\n<span class=\"go\"> "age": 26</span>\n<span class=\"go\">}</span>\n</pre></div><p>A complete description of <code>json</code> module -\nincluding write/read functions directly to/from files -\nis in the <a href=\"https://docs.python.org/3/library/json.html\">documentation</a>.</p>\n\n\n " } } }