|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# 33. Birthday presents\n", |
| 8 | + "\n", |
| 9 | + "## Reading and writing *.txt* files\n", |
| 10 | + "\n", |
| 11 | + "[Learn Python with Jupyter](https://learnpythonwithjupyter.com/) by [Serena Bonaretti](https://sbonaretti.github.io/) \n", |
| 12 | + "Narrative license: [CC BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/2.0/). Code license: [GNU-GPL v3](https://www.gnu.org/licenses/gpl-3.0.en.html) \n", |
| 13 | + "\n", |
| 14 | + "---" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "- Three of your friends celebrated their birthday this month, and you bought them presents online.\n", |
| 22 | + "Now, it’s time to perform a purchase analysis and save it in your records. The purchase amounts\n", |
| 23 | + "are in the file *33_purchases.txt*" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "markdown", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "---\n", |
| 31 | + "## 1. Reading a .txt file\n", |
| 32 | + "\n", |
| 33 | + "- Write a function that reads a `.txt` file containing one number per row and stores the numbers into a list:" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": null, |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [], |
| 41 | + "source": [ |
| 42 | + "def read_txt (file_name_in):\n", |
| 43 | + " \"\"\"Reads a .txt file with one number per row and returns them as a list\n", |
| 44 | + " \n", |
| 45 | + " Parameters\n", |
| 46 | + " ----------\n", |
| 47 | + " file_name_in: string\n", |
| 48 | + " Name of the file to read\n", |
| 49 | + " \n", |
| 50 | + " Returns\n", |
| 51 | + " -------\n", |
| 52 | + " numbers : list\n", |
| 53 | + " File content in a list of numbers\n", |
| 54 | + " \"\"\"\n", |
| 55 | + "\n", |
| 56 | + " # initialize output\n", |
| 57 | + " numbers = []\n", |
| 58 | + " \n", |
| 59 | + " # open the file\n", |
| 60 | + " with open(file_name_in, \"r\") as file:\n", |
| 61 | + " \n", |
| 62 | + " # read the file\n", |
| 63 | + " for line in file:\n", |
| 64 | + " print (\"line as read:\", line)\n", |
| 65 | + " \n", |
| 66 | + " # remove \"\\n\" from line\n", |
| 67 | + " line = line.rstrip(\"\\n\")\n", |
| 68 | + " print (\"line after stripping:\", line)\n", |
| 69 | + " print (\"-----\")\n", |
| 70 | + " \n", |
| 71 | + " # get only the non-empty lines\n", |
| 72 | + " if line != \"\":\n", |
| 73 | + " \n", |
| 74 | + " # transform the number to float\n", |
| 75 | + " number = float(line)\n", |
| 76 | + " \n", |
| 77 | + " # add to the output list\n", |
| 78 | + " numbers.append(number)\n", |
| 79 | + " \n", |
| 80 | + " # return the output\n", |
| 81 | + " return numbers\n", |
| 82 | + "\n", |
| 83 | + "# call the function and print the output\n", |
| 84 | + "purchases = read_txt(\"33_purchases.txt\")\n", |
| 85 | + "print (\"purchases:\", purchases)" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "- More compact alternative:" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": null, |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "def read_txt_compact (file_name_in):\n", |
| 102 | + " \"\"\"Reads a .txt file containing a column of numbers\n", |
| 103 | + " \n", |
| 104 | + " Parameters\n", |
| 105 | + " ----------\n", |
| 106 | + " file_name_in : string\n", |
| 107 | + " Name of the file to read\n", |
| 108 | + " \n", |
| 109 | + " Returns\n", |
| 110 | + " -------\n", |
| 111 | + " numbers : list\n", |
| 112 | + " File content in a list of numbers\n", |
| 113 | + " \"\"\"\n", |
| 114 | + " \n", |
| 115 | + " # open the file\n", |
| 116 | + " with open(file_name_in, \"r\") as file:\n", |
| 117 | + " \n", |
| 118 | + " # read the numbers and transform them into floats\n", |
| 119 | + " numbers = [float(number) for number in file.read().split()]\n", |
| 120 | + " \n", |
| 121 | + " # return the output\n", |
| 122 | + " return numbers\n", |
| 123 | + "\n", |
| 124 | + "# call the function and print the output\n", |
| 125 | + "purchases_compact = read_txt_compact(\"33_purchases.txt\")\n", |
| 126 | + "print (\"purchases:\", purchases_compact)" |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + "cell_type": "markdown", |
| 131 | + "metadata": {}, |
| 132 | + "source": [ |
| 133 | + "--- \n", |
| 134 | + "## 2. Analyzing the numbers" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "markdown", |
| 139 | + "metadata": {}, |
| 140 | + "source": [ |
| 141 | + "- Write a function that takes a list of numbers as input and returns the minimum, maximum, and sum as separate variables." |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": null, |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [], |
| 149 | + "source": [ |
| 150 | + "def calculate_stats(numbers): \n", |
| 151 | + " \"\"\"Returning minimum, maximum, and sum of a list of numbers \n", |
| 152 | + " \n", |
| 153 | + " Parameters\n", |
| 154 | + " ----------\n", |
| 155 | + " numbers: list \n", |
| 156 | + " Contains numbers\n", |
| 157 | + " \n", |
| 158 | + " Returns\n", |
| 159 | + " -------\n", |
| 160 | + " minimum : float\n", |
| 161 | + " Minimum of the list\n", |
| 162 | + " maximum : float \n", |
| 163 | + " Maximum of the list\n", |
| 164 | + " total : float\n", |
| 165 | + " Sum of the list numbers\n", |
| 166 | + " \"\"\"\n", |
| 167 | + " \n", |
| 168 | + " # calculate the minimum\n", |
| 169 | + " minimum = min(numbers)\n", |
| 170 | + "\n", |
| 171 | + " # calculate the maximum\n", |
| 172 | + " maximum = max(numbers) \n", |
| 173 | + "\n", |
| 174 | + " # calculate the sum\n", |
| 175 | + " total = sum(numbers)\n", |
| 176 | + "\n", |
| 177 | + " # return the stats\n", |
| 178 | + " return minimum, maximum, total\n", |
| 179 | + "\n", |
| 180 | + "# call the function\n", |
| 181 | + "mn, mx, tot = calculate_stats(purchases)\n", |
| 182 | + "print (\"minimum:\", mn)\n", |
| 183 | + "print (\"maximum:\", mx)\n", |
| 184 | + "print (\"total:\", tot)" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "markdown", |
| 189 | + "metadata": {}, |
| 190 | + "source": [ |
| 191 | + "---\n", |
| 192 | + "## 3. Saving the analysis \n", |
| 193 | + "\n", |
| 194 | + "- Create a function that given minimum, maximum, and total, writes them to file on three consecutive lines, specifying what they represent:" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": null, |
| 200 | + "metadata": {}, |
| 201 | + "outputs": [], |
| 202 | + "source": [ |
| 203 | + "def write_txt(file_name_out, minimum, maximum, total):\n", |
| 204 | + " \"\"\"Writing minimum, maximum, and sum to a file\n", |
| 205 | + " \n", |
| 206 | + " Parameters\n", |
| 207 | + " ----------\n", |
| 208 | + " file_name_out: string\n", |
| 209 | + " Name of the file to write\n", |
| 210 | + " minimum: float\n", |
| 211 | + " Minimum of the list\n", |
| 212 | + " maximum: float \n", |
| 213 | + " Maximum of the list\n", |
| 214 | + " total: float\n", |
| 215 | + " Sum of the numbers in the list\n", |
| 216 | + " \"\"\"\n", |
| 217 | + " \n", |
| 218 | + " # open the file to write\n", |
| 219 | + " with open(file_name_out, \"w\") as file:\n", |
| 220 | + " \n", |
| 221 | + " # write the file content\n", |
| 222 | + " file.write (\"minimum: \" + str(minimum) + \"\\n\")\n", |
| 223 | + " file.write (\"maximum: \" + str(maximum) + \"\\n\")\n", |
| 224 | + " file.write (\"total: \" + str(total))\n", |
| 225 | + "\n", |
| 226 | + "# call the function\n", |
| 227 | + "write_txt(\"33_purchases_stats.txt\", mn, mx, tot) " |
| 228 | + ] |
| 229 | + } |
| 230 | + ], |
| 231 | + "metadata": { |
| 232 | + "kernelspec": { |
| 233 | + "display_name": "Python 3 (ipykernel)", |
| 234 | + "language": "python", |
| 235 | + "name": "python3" |
| 236 | + }, |
| 237 | + "language_info": { |
| 238 | + "codemirror_mode": { |
| 239 | + "name": "ipython", |
| 240 | + "version": 3 |
| 241 | + }, |
| 242 | + "file_extension": ".py", |
| 243 | + "mimetype": "text/x-python", |
| 244 | + "name": "python", |
| 245 | + "nbconvert_exporter": "python", |
| 246 | + "pygments_lexer": "ipython3", |
| 247 | + "version": "3.9.6" |
| 248 | + }, |
| 249 | + "widgets": { |
| 250 | + "application/vnd.jupyter.widget-state+json": { |
| 251 | + "state": {}, |
| 252 | + "version_major": 2, |
| 253 | + "version_minor": 0 |
| 254 | + } |
| 255 | + } |
| 256 | + }, |
| 257 | + "nbformat": 4, |
| 258 | + "nbformat_minor": 4 |
| 259 | +} |
0 commit comments