update lecture material to move to notebook
[harrypotter-wikipedia-cdsw] / harrypotter_edit_trend.ipynb
diff --git a/harrypotter_edit_trend.ipynb b/harrypotter_edit_trend.ipynb
new file mode 100644 (file)
index 0000000..13d3d11
--- /dev/null
@@ -0,0 +1,102 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "from csv import DictReader"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# read in the input file and count by day\n",
+    "input_file = open(\"hp_wiki.tsv\", 'r', encoding=\"utf-8\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "edits_by_day = {}\n",
+    "for row in DictReader(input_file, delimiter=\"\\t\"):\n",
+    "    day_string = row['timestamp'][0:10]\n",
+    "\n",
+    "    if day_string in edits_by_day:\n",
+    "        edits_by_day[day_string] = edits_by_day[day_string] + 1\n",
+    "    else:\n",
+    "        edits_by_day[day_string] = 1"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "input_file.close()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# output the counts by day\n",
+    "output_file = open(\"hp_edits_by_day.tsv\", \"w\", encoding='utf-8')\n",
+    "\n",
+    "# write a header\n",
+    "print(\"date\\tedits\", file=output_file)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "# iterate through every day and print out data into the file\n",
+    "for day_string in edits_by_day.keys():\n",
+    "    print(\"\\t\".join([day_string, str(edits_by_day[day_string])]), file=output_file)\n",
+    "\n",
+    "output_file.close()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Python 3",
+   "language": "python",
+   "name": "python3"
+  },
+  "language_info": {
+   "codemirror_mode": {
+    "name": "ipython",
+    "version": 3
+   },
+   "file_extension": ".py",
+   "mimetype": "text/x-python",
+   "name": "python",
+   "nbconvert_exporter": "python",
+   "pygments_lexer": "ipython3",
+   "version": "3.7.3"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}

Benjamin Mako Hill || Want to submit a patch?