diff --git a/s24/Louis_Lecture_Notes/26_Files_and_Directories/Lec26_Files_and_Directories_Solution_Oliphant.ipynb b/s24/Louis_Lecture_Notes/26_Files_and_Directories/Lec26_Files_and_Directories_Solution_Oliphant.ipynb index 366eddaa1bb604b79ab1a82cfdc3ed437959b851..efcee2a9b7facfe916744423eef331e2ce36d3cd 100644 --- a/s24/Louis_Lecture_Notes/26_Files_and_Directories/Lec26_Files_and_Directories_Solution_Oliphant.ipynb +++ b/s24/Louis_Lecture_Notes/26_Files_and_Directories/Lec26_Files_and_Directories_Solution_Oliphant.ipynb @@ -8,7 +8,8 @@ "source": [ "# Warmup 0a: Run these!\n", "import os\n", - "from collections import namedtuple" + "from collections import namedtuple\n", + "import json" ] }, { @@ -17,9 +18,35 @@ "metadata": {}, "outputs": [], "source": [ - "# Warmup 0b:\n", - "# Give an example of a mutable object... list\n", - "# Give an example of an immutable object... tuple" + "# Warmup 0b: Run these to create functions for reading and writing JSON files.\n", + "# Deserialize\n", + "def read_json(path):\n", + " with open(path, encoding = \"utf-8\") as f: # f is a varaible \n", + " return json.load(f) # f represents data in the JSON file (dict, list, etc)\n", + " \n", + "# Serialize\n", + "def write_json(path, data):\n", + " with open(path, 'w', encoding = \"utf-8\") as f:\n", + " json.dump(data, f, indent = 2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 0c:\n", + "scores = {\n", + " 'john': [83,92,74],\n", + " 'sally': [88,97,81]\n", + "}\n", + "write_json('scores.json',scores)\n", + "\n", + "# TODO: What is the difference between the way the JSON file stores the scores data\n", + "# and the way it is written above in the scores variable?\n", + "\n", + "# ANSWER: Strings in a JSON file must be stored with double quotes not single quotes" ] }, { diff --git a/s24/Louis_Lecture_Notes/26_Files_and_Directories/Lec26_Files_and_Directories_Template_Oliphant.ipynb b/s24/Louis_Lecture_Notes/26_Files_and_Directories/Lec26_Files_and_Directories_Template_Oliphant.ipynb index cdf7f650087f27fcd6c797efc6cc8087759e3aee..572f86811d2aaa40177b1829c75463179f9b4257 100644 --- a/s24/Louis_Lecture_Notes/26_Files_and_Directories/Lec26_Files_and_Directories_Template_Oliphant.ipynb +++ b/s24/Louis_Lecture_Notes/26_Files_and_Directories/Lec26_Files_and_Directories_Template_Oliphant.ipynb @@ -2,24 +2,49 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Warmup 0a: Run these!\n", "import os\n", - "from collections import namedtuple" + "from collections import namedtuple\n", + "import json" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ - "# Warmup 0b:\n", - "# Give an example of a mutable object...\n", - "# Give an example of an immutable object..." + "# Warmup 0b: Run these to create functions for reading and writing JSON files.\n", + "# Deserialize\n", + "def read_json(path):\n", + " with open(path, encoding = \"utf-8\") as f: # f is a varaible \n", + " return json.load(f) # f represents data in the JSON file (dict, list, etc)\n", + " \n", + "# Serialize\n", + "def write_json(path, data):\n", + " with open(path, 'w', encoding = \"utf-8\") as f:\n", + " json.dump(data, f, indent = 2)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "# Warmup 0c:\n", + "scores = {\n", + " 'john': [83,92,74],\n", + " 'sally': [88,97,81]\n", + "}\n", + "write_json('scores.json',scores)\n", + "\n", + "# TODO: What is the difference between the way the JSON file stores the scores data\n", + "# and the way it is written above in the scores variable?" ] }, { diff --git a/s24/Louis_Lecture_Notes/26_Files_and_Directories/scores.json b/s24/Louis_Lecture_Notes/26_Files_and_Directories/scores.json new file mode 100644 index 0000000000000000000000000000000000000000..b6e7f9f8952cf307bc6b02a1c8ded1ab0bd464ac --- /dev/null +++ b/s24/Louis_Lecture_Notes/26_Files_and_Directories/scores.json @@ -0,0 +1,12 @@ +{ + "john": [ + 83, + 92, + 74 + ], + "sally": [ + 88, + 97, + 81 + ] +} \ No newline at end of file