summaryrefslogtreecommitdiff
path: root/python/atms-310/notebooks/Week 04 F.ipynb
blob: 00acbfe049da90bf193b820f0cf05e2129e6bb41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h1>8. Text I/O</h1>\n",
    "\n",
    "<h2>10/20/2023</h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>8.0 Last Time...</h2>\n",
    "<ul>\n",
    "    <li>You can use functions within NumPy to find the shape, number of dimensions, and number of elements in any array.</li>\n",
    "    <li><b>numpy.where()</b> will let you find the locations of elements within an array that meet a specified criterion.</li>\n",
    "    <li>Some functions within NumPy allow you to manipulate arrays, such as reshaping, transposing, \"unraveling\", concatenation, and repeating individual array elements.</li>\n",
    "    <li>When you're in a situation with multiple nested loops, often you can instead perform all operations a lot more easily by using array syntax.</li>\n",
    "    <li>Mathematical operations act on arrays elementwise.</li>\n",
    "    <li>When testing within an array, <b>and</b> and <b>or</b> cannot be used; instead, use NumPy's built-in functions.</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>8.1 File Objects</h2>\n",
    "\n",
    "A file object is just a variable that represents the file within Python. The process of creating a file object is the same general idea as creating any variable: you create it by assignment.\n",
    "\n",
    "For a text file, you can create a file with the built-in <b>open()</b> statement. The first argument in <b>open</b> gives the filename, and the second sets the mod for the file:\n",
    "<ul>\n",
    "    <li><b>'r'</b>: sets the file to read-only.</li>\n",
    "    <li><b>'w'</b>: sets the file to writing mode.</li>\n",
    "    <li><b>'a'</b>: sets the file to append mode (you can only add new things to the end).</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "_io.TextIOWrapper"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Try opening the 'test.txt' file that you added to your server.\n",
    "data = open(\"../test.txt\", \"r\")\n",
    "type(data)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you're done with a file, you can use the <b>close()</b> method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Close that file back up.\n",
    "data.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>8.2 Text Input/Output</h2>\n",
    "\n",
    "To read a line from a file into a variable, you can use the <b>readline()</b> method."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is a test!\n",
      "\n",
      "Here's some information:\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# First, open the file.\n",
    "data = open(\"../test.txt\",\"r\")\n",
    "\n",
    "\n",
    "# Assign the first line of text to the variable aline.\n",
    "aline = data.readline()\n",
    "\n",
    "# Calling readline() multiple times in a row will print the next row.\n",
    "bline = data.readline()\n",
    "\n",
    "# Print those first two lines of text.\n",
    "print(aline)\n",
    "print(bline)\n",
    "\n",
    "# Close the file. (This is good practice!)\n",
    "data.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also write a loop to go through the whole file!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "This is a test!\n",
      "\n",
      "Here's some information:\n",
      "\n",
      "IMPORTANT THINGS TO KNOW\n",
      "\n",
      "Okay, that's all I got.\n"
     ]
    }
   ],
   "source": [
    "data = open(\"../test.txt\", \"r\")\n",
    "\n",
    "for i in data:\n",
    "    print(i)\n",
    "    \n",
    "data.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Okay, but that's fairly limiting; more often, you'll want to read the whole file and put each line into a list as an element; this can be done using <b>readlines()</b> (note the plural!)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['This is a test!\\n', \"Here's some information:\\n\", 'IMPORTANT THINGS TO KNOW\\n', \"Okay, that's all I got.\"]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# Let's open the file again.\n",
    "data = open(\"../test.txt\", \"r\")\n",
    "\n",
    "\n",
    "# Save the file's contents to a list.\n",
    "contents = data.readlines()\n",
    "\n",
    "print(contents)\n",
    "print(type(contents))\n",
    "\n",
    "# Close that file!\n",
    "data.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that there's a newline (<b>\\n</b>) character at the end of each line (except the last one).\n",
    "\n",
    "To write to a file, you can use the <b>write()</b> method (obviously this doesn't work if a file is in read-only mode)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Let's open a file in writing mode.\n",
    "data = open(\"../test.txt\", \"w\")\n",
    "\n",
    "\n",
    "# Write a phrase to the file.\n",
    "data.write(\"hello world\")\n",
    "data.close()\n",
    "\n",
    "# i didnt run this, and dont ever run this. will overwrite file.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that this overwrites everything currently inside the file! To write multiple lines (in list format) to a file, use <b>writelines()</b>."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'_io.TextIOWrapper' object has no attribute 'append'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m/Users/nik/data/python/notebooks/Week 04 F.ipynb Cell 16\u001b[0m line \u001b[0;36m7\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/nik/data/python/notebooks/Week%2004%20F.ipynb#X21sZmlsZQ%3D%3D?line=4'>5</a>\u001b[0m data\u001b[39m.\u001b[39mwritelines(contents)\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/nik/data/python/notebooks/Week%2004%20F.ipynb#X21sZmlsZQ%3D%3D?line=5'>6</a>\u001b[0m data \u001b[39m=\u001b[39m \u001b[39mopen\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39m../test.txt\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39ma\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[0;32m----> <a href='vscode-notebook-cell:/Users/nik/data/python/notebooks/Week%2004%20F.ipynb#X21sZmlsZQ%3D%3D?line=6'>7</a>\u001b[0m data\u001b[39m.\u001b[39;49mappend(\u001b[39m\"\u001b[39m\u001b[39mpoopy booty butt balls\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m      <a href='vscode-notebook-cell:/Users/nik/data/python/notebooks/Week%2004%20F.ipynb#X21sZmlsZQ%3D%3D?line=7'>8</a>\u001b[0m data\u001b[39m.\u001b[39mclose()\n",
      "\u001b[0;31mAttributeError\u001b[0m: '_io.TextIOWrapper' object has no attribute 'append'"
     ]
    }
   ],
   "source": [
    "data = open(\"../test.txt\", \"w\")\n",
    "\n",
    "# Earlier in this notebook we saved the contents of our file to a variable 'contents'.\n",
    "\n",
    "data.writelines(contents)\n",
    "data = open(\"../test.txt\", \"a\")\n",
    "data.(\"poopy booty butt balls\")\n",
    "data.close()\n",
    "\n",
    "# ok whatever ill figure it out when i need to"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>8.3 Processing File Contents</h2>\n",
    "\n",
    "As you might imagine, the contents of files can be pretty unwieldy. Luckily, there are a lot of methods that will make data easier to read!\n",
    "\n",
    "Sometimes (as with .csv files) you'll want to take a string and break it into list using a particular separator. <b>split()</b> is a useful tool!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['3.4', '2.1', '-2.6']\n",
      "['3.4', '2.1', '-2.6']\n"
     ]
    }
   ],
   "source": [
    "# Let's create a single string that has three pieces of data in it.\n",
    "a = '3.4 2.1 -2.6'\n",
    "\n",
    "\n",
    "# The obvious choice for a separator is a space.\n",
    "print(a.split(\" \"))\n",
    "a = '3.4,2.1,-2.6'\n",
    "print(a.split(\",\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If everything we read from a file is a string, we're sometimes going to have to convert to integers or floats."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['3.4', '2.1', '-2.6']\n"
     ]
    }
   ],
   "source": [
    "# We'll need NumPy for this!\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "# Let's look at a typical situation: we've grabbed some numbers from a csv file.\n",
    "a = '3.4,2.1,-2.6'\n",
    "a = a.split(\",\")\n",
    "\n",
    "# Note that these are still strings.\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3.4  2.1 -2.6]\n"
     ]
    }
   ],
   "source": [
    "# We can convert these to floats the way we did before!\n",
    "b = np.zeros(len(a))\n",
    "for i in range(len(a)):\n",
    "    b[i] = float(a[i])\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alternatively, we can convert to an array and use the <b>astype()</b> function built-in there. <b>'d'</b> is a float (double-precision), <b>'l'</b> is an integer (long integer)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3.4  2.1 -2.6]\n"
     ]
    }
   ],
   "source": [
    "bnum = np.array(a).astype(\"d\")\n",
    "print(bnum)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2>8.4 Take-Home Points</h2>\n",
    "<ul>\n",
    "    <li>The <b>open()</b> statement lets you open a file in read, write, or append mode.</li>\n",
    "    <li>Files should always be closed using the <b>close()</b> statement.</li>\n",
    "    <li>You can read a single line with <b>readline()</b>, and multiple lines with <b>readlines()</b>.</li>\n",
    "    <li>The <b>write()</b> method allows you to write a single line, and the <b>writelines()</b> method allows you to write multiple lines.</li>\n",
    "    <li><b>split()</b> lets you break strings based on defined separators.</li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# remember to close files\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}