{ "cells": [ { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [], "source": [ "(require '[clojure.data.csv :as csv]\n", " '[clojure.edn :as edn]\n", " '[clojure.java.io :as io])" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "#'user/data-food-csv" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(def data-food-csv \"/home/didier/gitlab/honore-beaugrand/resources/data/food-data.csv\")" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#'user/read-food-data-from-csv" ] }, "execution_count": 125, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(defn map-values [f m]\n", " (into {} (for [[k v] m] [k (f v)])))\n", "\n", "(defn beautiful-keyword [s]\n", " (-> s\n", " (clojure.string/replace \"(\" \"\")\n", " (clojure.string/replace \")\" \"\")\n", " (clojure.string/replace \" \" \"_\")\n", " clojure.string/lower-case\n", " keyword))\n", " \n", " \n", "(defn csv-data->maps [csv-data]\n", " (map zipmap\n", " (->> (first csv-data) ;; First row is the header\n", " (map beautiful-keyword) ;; Drop if you want string keys instead\n", " repeat)\n", " (rest csv-data)))\n", "\n", "(defn cast-value [v]\n", " (cond\n", " (= v \"NULL\") nil\n", " (some? (re-matches #\"-?\\d+(.\\d*)?\" v)) (Double/parseDouble v)\n", " :else v))\n", "\n", "(defn read-food-data-from-csv [csv-file-path]\n", " (with-open [reader (io/reader csv-file-path)]\n", " (->> reader\n", " csv/read-csv\n", " csv-data->maps\n", " (map #(map-values cast-value %))\n", " vec))) \n" ] }, { "cell_type": "code", "execution_count": 126, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "#'user/all-data" ] }, "execution_count": 126, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(def all-data (read-food-data-from-csv data-food-csv))" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [], "source": [ "(def all-data-string (prn-str all-data))\n", "(spit \"/home/didier/gitlab/honore-beaugrand/resources/data/food-data.edn\" all-data-string)" ] } ], "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 }