first commit
This commit is contained in:
commit
6202bbfdd5
13 files changed
+1352
No files matched your search
@@ -0,0 +1,193 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"First, we need to load the [cheshire](https://github.com/dakrone/cheshire) module to load json."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"(require '[cheshire.core :as cheshire])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Second, we define a variable that points to the url we wished to download"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"#'user/election-results-url"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"(def election-results-url \"https://dgeq.org/doc/gen7-4-2014/resultats.json\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Then, we download the content of the url as a \"string\". See the [slurp](https://clojuredocs.org/clojure.core/slurp) function to see how it works."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"#'user/raw-data"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"(def raw-data (slurp election-results-url))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now, let's print some information about the downloaded string...\n",
|
||||
"\n",
|
||||
"We can use [subs](https://clojuredocs.org/clojure.core/subs) for that"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"the length of the string is... 193056\n",
|
||||
"the string starts with... {\"statistiques\":{\"partisPolitiques\":[{\"numeroParti\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"(println \"the length of the string is...\" (count raw-data))\n",
|
||||
"(println \"the string starts with...\" (subs raw-data 0 50))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We can now use cheshire to load the data. The `parse-string` functions looks awesome to do that."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"#'user/data"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"(def data (cheshire/parse-string raw-data true))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We can now explore the real data structure.."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"the data structure type is... clojure.lang.PersistentArrayMap\n",
|
||||
"the keys are... (:statistiques :circonscriptions)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"(println \"the data structure type is... \" (type data))\n",
|
||||
"(println \"the keys are... \" (keys data))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We can now use the clojure inverse of `slurp`, the [spit](https://clojuredocs.org/clojure.core/spit) function, to serialize the data to a file. The file should be named `resources/data/election-2014.edn`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"(spit \"resources/data/election-2014.edn\" (str data))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Lein-Clojure",
|
||||
"language": "clojure",
|
||||
"name": "lein-clojure"
|
||||
},
|
||||
"language_info": {
|
||||
"file_extension": ".clj",
|
||||
"mimetype": "text/x-clojure",
|
||||
"name": "clojure",
|
||||
"version": "1.9.0"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in new issue
Block a user