75 |
75 |
76 pend |
76 pend |
77 |
77 |
78 Following is an exercise that you must do. |
78 Following is an exercise that you must do. |
79 |
79 |
80 .. #[[Anoop:add context switch to next slide - questions]] |
80 {{ show slide with Question 1 }} |
81 |
81 |
82 %%1%% Split the variable into a list, ``pend_list``, of the lines in |
82 %%1%% Split the variable into a list, ``pend_list``, of the lines in |
83 the file. Hint, use the tab command to see what methods the string |
83 the file. Hint, use the tab command to see what methods the string |
84 variable has. |
84 variable has. |
85 |
85 |
86 Please, pause the video here. Do the exercise and then continue. |
86 Please, pause the video here. Do the exercise and then continue. |
87 |
87 |
88 .. #[punch: should this even be put? add dependency to strings LO, |
88 {{ show slide with Solution 1 }} |
89 .. where we mention that strings have methods for manipulation. hint: |
|
90 .. use splitlines()] |
|
91 |
|
92 .. #[[Anoop: let us have it here, let us consider this as a |
|
93 refresher]] |
|
94 |
|
95 .. #[[Anoop:add context switch to next slide - solution]] |
|
96 |
89 |
97 :: |
90 :: |
98 |
91 |
99 pend_list = pend.splitlines() |
92 pend_list = pend.splitlines() |
100 |
93 |
101 pend_list |
94 pend_list |
102 |
95 |
103 Now, let us learn to read the file line-by-line. But, before that we |
96 Now, let us learn to read the file line-by-line. But, before that we |
104 will have to close the file, since the file has already been read till |
97 will have to close the file, since the file has already been read till |
105 the end. |
98 the end. |
106 |
|
107 .. #[punch: should we mention file-pointer?] |
|
108 |
|
109 .. #[[Anoop: I think we can say that ``f`` is a file pointer which |
|
110 points to the next line/data to be read from the file. We could |
|
111 skip details.]] |
|
112 |
99 |
113 Let us close the file opened into f. |
100 Let us close the file opened into f. |
114 :: |
101 :: |
115 |
102 |
116 f.close() |
103 f.close() |
144 :: |
131 :: |
145 |
132 |
146 for line in f: |
133 for line in f: |
147 print line |
134 print line |
148 |
135 |
149 As we already know, ``line`` is just a dummy variable, and not a |
136 As we already know, ``line`` is variable, sometimes called the loop |
150 keyword. We could have used any other variable name, but ``line`` |
137 variable, and it is not a keyword. We could have used any other |
151 seems meaningful enough. |
138 variable name, but ``line`` seems meaningful enough. |
152 |
|
153 .. #[[Anoop: using dummy variable doesn't seem correct, can say line |
|
154 is a variable]] |
|
155 |
139 |
156 Instead of just printing the lines, let us append them to a list, |
140 Instead of just printing the lines, let us append them to a list, |
157 ``line_list``. We first initialize an empty list, ``line_list``. |
141 ``line_list``. We first initialize an empty list, ``line_list``. |
158 :: |
142 :: |
159 |
143 |
179 Notice that ``line_list`` is a list of the lines in the file, along |
163 Notice that ``line_list`` is a list of the lines in the file, along |
180 with the newline characters. If you noticed, ``pend_list`` did not |
164 with the newline characters. If you noticed, ``pend_list`` did not |
181 contain the newline characters, because the string ``pend`` was |
165 contain the newline characters, because the string ``pend`` was |
182 split on the newline characters. |
166 split on the newline characters. |
183 |
167 |
184 .. #[[Anoop: I think we need to tell them that each line can be |
168 Using some string methods, that we shall look at in the tutorial on |
185 stripped and appended to list to avoid the problem of newline |
169 strings, we can strip out the newline characters from the lines. |
186 characters.]] |
|
187 |
170 |
188 .. #[[Anoop: I think the code that are required to be typed can be |
171 .. #[[Anoop: I think the code that are required to be typed can be |
189 added to the slide.]] |
172 added to the slide.]] |
190 |
|
191 .. #[[Anoop: Context switches are to be added.]] |
|
192 |
173 |
193 {{{ show the summary slide }}} |
174 {{{ show the summary slide }}} |
194 |
175 |
195 That brings us to the end of this tutorial. In this tutorial we |
176 That brings us to the end of this tutorial. In this tutorial we |
196 have learnt to open and close files, read the data in the files as |
177 have learnt to open and close files, read the data in the files as |