summaryrefslogtreecommitdiff
path: root/slides/basic_python/io_files_parsing.tex
blob: f766f1c19a7605095ab60c96bd12ecdaf5c50872 (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
\section{I/O}

\begin{frame}[fragile]
  \frametitle{Printing}
  \begin{lstlisting}
  In[]: a = "This is a string"
  In[]: a
  In[]: print a
  \end{lstlisting}
  \begin{itemize}
  \item Both \texttt{a}, and \texttt{print a} are showing the value
  \item What is the difference?
  \item Typing \texttt{a} shows the value; \texttt{print a} prints it
  \item Typing \texttt{a} shows the value only in interpreter
  \item In a script, it has no effect. 
  \end{itemize}
  \begin{lstlisting}
  In[]: b = "A line \n New line"
  In[]: b
  In[]: print b
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{String formatting}
  \small
  \begin{lstlisting}
  In[]: x = 1.5
  In[]: y = 2
  In[]: z = "zed"
  In[]: print "x is %2.1f y is %d z is %s" %(x, y, z)
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\texttt{print x} \& \texttt{print x,}}
  \begin{itemize}
  \item Open an editor
  \item Type the following code
  \item Save as \texttt{print\_example.py}
  \end{itemize}
  \begin{lstlisting}
   In[]: print "Hello"
   In[]: print "World"

   In[]: print "Hello",
   In[]: print "World"
  \end{lstlisting}
  \begin{itemize}
  \item Run the script using \texttt{\% run print\_example.py}
  \item \texttt{print x} adds a newline whereas \texttt{print x,} adds
    a space
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\texttt{raw\_input}}
  \begin{lstlisting}
  In[]: ip = raw_input()    
  \end{lstlisting}
  \begin{itemize}
  \item The cursor is blinking; waiting for input
  \item Type \texttt{an input} and hit <ENTER>
  \end{itemize}
  \begin{lstlisting}
   In[]: print ip
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\texttt{raw\_input} \ldots}
  \begin{lstlisting}
  In[]: c = raw_input()
  5.6
  In[]: c
  In[]: type(c)
  \end{lstlisting}
  \begin{itemize}
  \item \alert{\texttt{raw\_input} always takes a string}
  \end{itemize}
  \begin{lstlisting}
  In[]: name = raw_input("Enter your name: ")
  Enter your name:  George
  \end{lstlisting}
  \begin{itemize}
  \item \texttt{raw\_input} can display a prompt string for the user
  \end{itemize}
\end{frame}

\section{Files}

\begin{frame}[fragile]
  \frametitle{Opening files}
  \begin{lstlisting}
    pwd # present working directory
    cd /home/fossee # go to location of the file
  \end{lstlisting}
  {\tiny The file is in our present working directory}
  \begin{lstlisting}
   In[]: f = open('pendulum.txt')
   In[]: f
  \end{lstlisting}
  \begin{itemize}
  \item \texttt{f} is a file object
  \item Shows the mode in which the file is open (read mode)
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Reading the whole file}
  \begin{lstlisting}
   In[]: pend = f.read()
   In[]: print pend
  \end{lstlisting}
  \begin{itemize}
  \item We have read the whole file into the variable \texttt{pend}
  \end{itemize}
  \begin{lstlisting}
   In[]: type(pend)
   In[]: pend_list = pend.splitlines()
   In[]: pend_list
  \end{lstlisting}
  \begin{itemize}
  \item  \texttt{pend} is a string variable
  \item We can split it at the newline characters into a list of
    strings
  \item Close the file, when done; Also, if you want to read again
  \end{itemize}
  \begin{lstlisting}
   In[]: f.close()
   In[]: f
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Reading line-by-line}
  \begin{lstlisting}
   In[]: for line in open('pendulum.txt'):
   ....:     print line
  \end{lstlisting}
  \begin{itemize}
  \item The file object is an ``iterable''
  \item We iterate over it and print each line
  \item Instead of printing, collect lines in a list
  \end{itemize}
  \begin{lstlisting}
   In[]: line_list = [ ]
   In[]: for line in open('pendulum.txt'):
   ....:     line_list.append(line)
  \end{lstlisting}
\end{frame}


\begin{frame}[fragile]
  \frametitle{File parsing -- Problem}
  \begin{lstlisting}
    A;010002;ANAND R;058;037;42;35;40;212;P;;
  \end{lstlisting}
  \begin{itemize}
  \item File with records like the one above is given
  \item Each record has fields separated by \verb+;+
  \item region code; roll number; name; 
  \item marks --- $1^{st}$ L; $2^{nd}$ L; math; science; social; total
  \item pass/fail indicated by P/F; W if withheld and else empty
    \end{itemize}

  \begin{itemize}
  \item We wish to calculate mean of math marks in region B
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Tokenization}
  \begin{lstlisting}
  In[]: line = "parse this           string"
  In[]: line.split()
  \end{lstlisting}
  \begin{itemize}
  \item Original string is split on white-space (if no argument)
  \item Returns a list of strings
  \item It can be given an argument to split on that argrument
  \end{itemize}
  \begin{lstlisting}
   In[]: record = "A;015163;JOSEPH RAJ S;083;042;47;AA;72;244;;;"
   In[]: record.split(';')
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]
  \frametitle{Tokenization \ldots}
  \begin{itemize}
  \item Since we split on commas, fields may have extra spaces at ends
  \item We can strip out the spaces at the ends
  \end{itemize}
  \begin{lstlisting}
   In[]: word = "     B    "
   In[]: word.strip()
  \end{lstlisting}
  \begin{itemize}
  \item \texttt{strip} is returning a new string 
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{\texttt{str} to \texttt{float}}
  \begin{itemize}
  \item After tokenizing, the marks we have are strings
  \item We need numbers to perform math operations
  \end{itemize}
  \begin{lstlisting}
  In[]: mark_str = "1.25"
  In[]: mark = int(mark_str)
  In[]: type(mark_str)
  In[]: type(mark)
  \end{lstlisting}
  \begin{itemize}
  \item \texttt{strip} is returning a new string 
  \end{itemize}
\end{frame}

\begin{frame}[fragile]
  \frametitle{File parsing -- Solution}
  \begin{lstlisting}
  In[]: math_B = [] # empty list to store marks
  In[]: for line in open("sslc1.txt"):
  ....:     fields = line.split(";")

  ....:     reg_code = fields[0]
  ....:     reg_code_clean = reg_code.strip()

  ....:     math_mark_str = fields[5]
  ....:     math_mark = float(math_mark_str)

  ....:     if reg_code == "B":
  ....:         math_B.append(math_mark)

  In[]: math_B_mean = sum(math_B) / len(math_B)
  In[]: math_B_mean
  \end{lstlisting}
\end{frame}