|
23 | 23 | "metadata": {}, |
24 | 24 | "source": [ |
25 | 25 | "## Example: Editing a string\n", |
26 | | - "SOUNDS LIKE I'M YELLING! to Sounds like I'm NOT yelling\n", |
| 26 | + "Goal: convert \"SOUNDS LIKE I'M YELLING!\" to \"Sounds like I'm NOT yelling\"\n", |
27 | 27 | "\n", |
28 | 28 | "Problem break down:\n", |
29 | 29 | "- First do the conversion to lower case\n", |
|
32 | 32 | "- Next, stick in a \"not\"\n", |
33 | 33 | "- Next, take the ! out\n", |
34 | 34 | "\n", |
35 | | - "Obviously, if I knew what the sentence was it would be a lot easier to just, well, make the new sentence... But imagine, instead, you're writing a general purpose routine to convert all-caps strings to lower-case ones..." |
| 35 | + "Obviously, if I knew what the sentence was it would be a lot easier to just, well, make the new sentence... But imagine, instead, you're writing a general purpose routine to convert all-caps strings to lower-case ones and fix grammar errors...\n", |
| 36 | + "\n", |
| 37 | + "TODO\n", |
| 38 | + "- Try taking out all of the spaces" |
36 | 39 | ] |
37 | 40 | }, |
38 | 41 | { |
39 | 42 | "cell_type": "code", |
40 | | - "execution_count": 1, |
| 43 | + "execution_count": null, |
41 | 44 | "metadata": {}, |
42 | 45 | "outputs": [], |
43 | 46 | "source": [ |
|
47 | 50 | }, |
48 | 51 | { |
49 | 52 | "cell_type": "code", |
50 | | - "execution_count": 2, |
| 53 | + "execution_count": null, |
51 | 54 | "metadata": {}, |
52 | | - "outputs": [ |
53 | | - { |
54 | | - "name": "stdout", |
55 | | - "output_type": "stream", |
56 | | - "text": [ |
57 | | - "This is yelling: SOUNDS LIKE I'M YELLING!, this is not: sounds like i'm yelling!\n" |
58 | | - ] |
59 | | - } |
60 | | - ], |
| 55 | + "outputs": [], |
61 | 56 | "source": [ |
62 | 57 | "# convert to lower case - lower makes a copy of the string, converts the upper case letters, then returns the copy\n", |
63 | 58 | "my_string_lower_case = my_string_upper_case.lower()\n", |
|
68 | 63 | }, |
69 | 64 | { |
70 | 65 | "cell_type": "code", |
71 | | - "execution_count": 3, |
| 66 | + "execution_count": null, |
72 | 67 | "metadata": {}, |
73 | | - "outputs": [ |
74 | | - { |
75 | | - "name": "stdout", |
76 | | - "output_type": "stream", |
77 | | - "text": [ |
78 | | - "Sounds like i'm yelling!\n" |
79 | | - ] |
80 | | - } |
81 | | - ], |
| 68 | + "outputs": [], |
82 | 69 | "source": [ |
83 | | - "# Oops, should capitalize first letter:\n", |
| 70 | + "# Oops, should capitalize first letter - note capitalize, and not upper:\n", |
84 | 71 | "my_string_cap = my_string_lower_case.capitalize()\n", |
85 | 72 | "print(my_string_cap)" |
86 | 73 | ] |
87 | 74 | }, |
88 | 75 | { |
89 | 76 | "cell_type": "code", |
90 | | - "execution_count": 4, |
| 77 | + "execution_count": null, |
91 | 78 | "metadata": {}, |
92 | | - "outputs": [ |
93 | | - { |
94 | | - "name": "stdout", |
95 | | - "output_type": "stream", |
96 | | - "text": [ |
97 | | - "Sounds like I'm yelling!\n" |
98 | | - ] |
99 | | - } |
100 | | - ], |
| 79 | + "outputs": [], |
101 | 80 | "source": [ |
102 | 81 | "# ... and replace the lower-case i with an upper case\n", |
103 | 82 | "# Notice the use of \"\" so that we can use ' as a character\n", |
|
107 | 86 | }, |
108 | 87 | { |
109 | 88 | "cell_type": "code", |
110 | | - "execution_count": 5, |
| 89 | + "execution_count": null, |
111 | 90 | "metadata": {}, |
112 | | - "outputs": [ |
113 | | - { |
114 | | - "name": "stdout", |
115 | | - "output_type": "stream", |
116 | | - "text": [ |
117 | | - "Sounds like I'm not yelling!\n" |
118 | | - ] |
119 | | - } |
120 | | - ], |
| 91 | + "outputs": [], |
121 | 92 | "source": [ |
122 | 93 | "# add in the \"not\" by finding the last space\n", |
123 | 94 | "index_last_space = my_string_with_i_fixed.rfind(\" \")\n", |
|
130 | 101 | }, |
131 | 102 | { |
132 | 103 | "cell_type": "code", |
133 | | - "execution_count": 6, |
| 104 | + "execution_count": null, |
134 | 105 | "metadata": {}, |
135 | | - "outputs": [ |
136 | | - { |
137 | | - "name": "stdout", |
138 | | - "output_type": "stream", |
139 | | - "text": [ |
140 | | - "Sounds like I'm not yelling\n" |
141 | | - ] |
142 | | - } |
143 | | - ], |
| 106 | + "outputs": [], |
144 | 107 | "source": [ |
145 | 108 | "# ... without the ! at the end - use :-1 to only copy up to the last element\n", |
146 | 109 | "my_not_yelling_string_better = my_string_with_i_fixed[0:index_last_space] + \" not\" + my_string_with_i_fixed[index_last_space:-1]\n", |
147 | 110 | "print(my_not_yelling_string_better)" |
148 | 111 | ] |
149 | 112 | }, |
| 113 | + { |
| 114 | + "cell_type": "markdown", |
| 115 | + "metadata": {}, |
| 116 | + "source": [ |
| 117 | + "... Yes, you can do all of that editing in one line of really confusing Python. Just don't. BUT, you might try combining two steps at the same time..." |
| 118 | + ] |
| 119 | + }, |
150 | 120 | { |
151 | 121 | "cell_type": "markdown", |
152 | 122 | "metadata": {}, |
|
160 | 130 | }, |
161 | 131 | { |
162 | 132 | "cell_type": "code", |
163 | | - "execution_count": 7, |
| 133 | + "execution_count": null, |
164 | 134 | "metadata": {}, |
165 | | - "outputs": [ |
166 | | - { |
167 | | - "ename": "TypeError", |
168 | | - "evalue": "'str' object does not support item assignment", |
169 | | - "output_type": "error", |
170 | | - "traceback": [ |
171 | | - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
172 | | - "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", |
173 | | - "\u001b[0;32m<ipython-input-7-a8cfd7393bb6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmy_not_yelling_string\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m's'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
174 | | - "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" |
175 | | - ] |
176 | | - } |
177 | | - ], |
| 135 | + "outputs": [], |
178 | 136 | "source": [ |
179 | 137 | "my_not_yelling_string[1] = 's'" |
180 | 138 | ] |
|
189 | 147 | "\n", |
190 | 148 | "For these examples we'll have pre-defined strings that are \"typed in\" (rather than reading them from a file) just to keep things simple. In reality, you usually have to do this when you can't edit the files directly.\n", |
191 | 149 | "\n", |
192 | | - "An aside: When you only have one file you're \"cleaning up\" for processing (as opposed to writing some code to process a whole bunch of similar files) it is often a lot easier to open the file in your favorite spreadsheet editor and just... edit it there. " |
| 150 | + "An aside: When you only have one file you're \"cleaning up\" for processing (as opposed to writing some code to process a whole bunch of similar files) it is often a lot easier to open the file in your favorite spreadsheet editor and just... edit it there. \n", |
| 151 | + "\n", |
| 152 | + "TODO:\n", |
| 153 | + "- Create a string with semi-colons separating the values\n", |
| 154 | + "- Use split to pull it apart into a list of numbers" |
193 | 155 | ] |
194 | 156 | }, |
195 | 157 | { |
|
202 | 164 | }, |
203 | 165 | { |
204 | 166 | "cell_type": "code", |
205 | | - "execution_count": 8, |
| 167 | + "execution_count": null, |
206 | 168 | "metadata": {}, |
207 | | - "outputs": [ |
208 | | - { |
209 | | - "name": "stdout", |
210 | | - "output_type": "stream", |
211 | | - "text": [ |
212 | | - "\t0.3, 0.4, 0.5, 3., 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2\n" |
213 | | - ] |
214 | | - } |
215 | | - ], |
| 169 | + "outputs": [], |
216 | 170 | "source": [ |
217 | 171 | "# Make a list of numbers\n", |
218 | 172 | "list_of_numbers = [0.3 + 0.1 * n for n in range(0, 10)]\n", |
|
226 | 180 | }, |
227 | 181 | { |
228 | 182 | "cell_type": "code", |
229 | | - "execution_count": 9, |
| 183 | + "execution_count": null, |
230 | 184 | "metadata": {}, |
231 | | - "outputs": [ |
232 | | - { |
233 | | - "name": "stdout", |
234 | | - "output_type": "stream", |
235 | | - "text": [ |
236 | | - "[0.3, 0.4, 0.5, 3.0, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2]\n" |
237 | | - ] |
238 | | - } |
239 | | - ], |
| 185 | + "outputs": [], |
240 | 186 | "source": [ |
241 | 187 | "# Take it back apart again using split, looking for commans\n", |
242 | 188 | "my_list_back_as_strs = str_list_of_numbers.split(',')\n", |
|
248 | 194 | }, |
249 | 195 | { |
250 | 196 | "cell_type": "code", |
251 | | - "execution_count": 10, |
| 197 | + "execution_count": null, |
252 | 198 | "metadata": {}, |
253 | 199 | "outputs": [], |
254 | 200 | "source": [ |
|
267 | 213 | "We have a list of strings, which consist of a descriptive name followed by a colon followed by a number\n", |
268 | 214 | " - Find the end of the line (the \\n) then split up each line.\n", |
269 | 215 | " - If the line has a : then get out the number, otherwise, ignore it\n", |
270 | | - " - Note that there is a more complete example of this (for building a dictionary) in the dictionary tutorial" |
| 216 | + " - Note that there is a more complete example of this (for building a dictionary) in the dictionary tutorial\n", |
| 217 | + " \n", |
| 218 | + "TODO\n", |
| 219 | + "- Try printing out which item it is when you find one\n", |
| 220 | + "- To test it, reverse the order in the string and see if it still works" |
271 | 221 | ] |
272 | 222 | }, |
273 | 223 | { |
274 | 224 | "cell_type": "code", |
275 | | - "execution_count": 12, |
| 225 | + "execution_count": null, |
276 | 226 | "metadata": {}, |
277 | | - "outputs": [ |
278 | | - { |
279 | | - "name": "stdout", |
280 | | - "output_type": "stream", |
281 | | - "text": [ |
282 | | - "# This is a header\n", |
283 | | - "Item 1: 0.3\n", |
284 | | - "Blank line\n", |
285 | | - "Item 2: 1.3\n", |
286 | | - "\n" |
287 | | - ] |
288 | | - } |
289 | | - ], |
| 227 | + "outputs": [], |
290 | 228 | "source": [ |
291 | 229 | "my_str_to_parse = \"# This is a header\\nItem 1: 0.3\\nBlank line\\nItem 2: 1.3\\n\"\n", |
292 | 230 | "print(my_str_to_parse)" |
293 | 231 | ] |
294 | 232 | }, |
295 | 233 | { |
296 | 234 | "cell_type": "code", |
297 | | - "execution_count": 14, |
| 235 | + "execution_count": null, |
298 | 236 | "metadata": {}, |
299 | | - "outputs": [ |
300 | | - { |
301 | | - "name": "stdout", |
302 | | - "output_type": "stream", |
303 | | - "text": [ |
304 | | - "['# This is a header', 'Item 1: 0.3', 'Blank line', 'Item 2: 1.3', '']\n" |
305 | | - ] |
306 | | - } |
307 | | - ], |
| 237 | + "outputs": [], |
308 | 238 | "source": [ |
309 | 239 | "# First, split the line into strings separated by \\n\n", |
310 | 240 | "my_lines = my_str_to_parse.split('\\n')\n", |
|
313 | 243 | }, |
314 | 244 | { |
315 | 245 | "cell_type": "code", |
316 | | - "execution_count": 15, |
| 246 | + "execution_count": null, |
317 | 247 | "metadata": {}, |
318 | | - "outputs": [ |
319 | | - { |
320 | | - "name": "stdout", |
321 | | - "output_type": "stream", |
322 | | - "text": [ |
323 | | - "[0.3, 1.3]\n" |
324 | | - ] |
325 | | - } |
326 | | - ], |
| 248 | + "outputs": [], |
327 | 249 | "source": [ |
328 | 250 | "# An empty list of numbers\n", |
329 | 251 | "my_numbers = []\n", |
|
334 | 256 | " # split again - you could also use find and the resulting index\n", |
335 | 257 | " two_parts = l.split(\":\")\n", |
336 | 258 | " # Don't forget to convert the string to the number\n", |
| 259 | + " #. the [-1] is to get the last (second) element in the list\n", |
337 | 260 | " the_number = float(two_parts[-1])\n", |
338 | 261 | " my_numbers.append(the_number)\n", |
339 | 262 | "print(my_numbers)" |
|
344 | 267 | "metadata": {}, |
345 | 268 | "source": [ |
346 | 269 | "## Tutorial: Editing a file name\n", |
347 | | - "This is pretty common - take the last 4 characters off and replace them with a different file type" |
| 270 | + "This is pretty common - take the last 4 characters off and replace them with a different file type. Try changing .txt to .py and edit the second line to do the right thing." |
348 | 271 | ] |
349 | 272 | }, |
350 | 273 | { |
351 | 274 | "cell_type": "code", |
352 | | - "execution_count": 17, |
| 275 | + "execution_count": null, |
353 | 276 | "metadata": {}, |
354 | | - "outputs": [ |
355 | | - { |
356 | | - "name": "stdout", |
357 | | - "output_type": "stream", |
358 | | - "text": [ |
359 | | - "my_file.csv\n" |
360 | | - ] |
361 | | - } |
362 | | - ], |
| 277 | + "outputs": [], |
363 | 278 | "source": [ |
364 | 279 | "file_name_orig = \"my_file.txt\"\n", |
365 | 280 | "file_name_new = file_name_orig[0:-4] + \".csv\"\n", |
|
0 commit comments