summaryrefslogtreecommitdiff
path: root/slides/basic_python/io_files_parsing.tex
diff options
context:
space:
mode:
Diffstat (limited to 'slides/basic_python/io_files_parsing.tex')
-rw-r--r--slides/basic_python/io_files_parsing.tex69
1 files changed, 35 insertions, 34 deletions
diff --git a/slides/basic_python/io_files_parsing.tex b/slides/basic_python/io_files_parsing.tex
index cb46cbe..f766f1c 100644
--- a/slides/basic_python/io_files_parsing.tex
+++ b/slides/basic_python/io_files_parsing.tex
@@ -3,9 +3,9 @@
\begin{frame}[fragile]
\frametitle{Printing}
\begin{lstlisting}
- In[]: a = "This is a string"
- In[]: a
- In[]: print a
+ 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
@@ -15,19 +15,20 @@
\item In a script, it has no effect.
\end{itemize}
\begin{lstlisting}
- In[]: b = "A line \n New line"
- In[]: b
- In[]: print b
+ 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)
+ 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}
@@ -42,7 +43,7 @@
In[]: print "Hello"
In[]: print "World"
- In[]: print "Hello",
+ In[]: print "Hello",
In[]: print "World"
\end{lstlisting}
\begin{itemize}
@@ -55,7 +56,7 @@
\begin{frame}[fragile]
\frametitle{\texttt{raw\_input}}
\begin{lstlisting}
- In[]: ip = raw_input()
+ In[]: ip = raw_input()
\end{lstlisting}
\begin{itemize}
\item The cursor is blinking; waiting for input
@@ -69,17 +70,17 @@
\begin{frame}[fragile]
\frametitle{\texttt{raw\_input} \ldots}
\begin{lstlisting}
- In[]: c = raw_input()
- In[]: 5.6
+ In[]: c = raw_input()
+ 5.6
In[]: c
- In[]: type(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("Please enter your name: ")
- George
+ 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
@@ -126,7 +127,7 @@
\item Close the file, when done; Also, if you want to read again
\end{itemize}
\begin{lstlisting}
- In[]: f.close()
+ In[]: f.close()
In[]: f
\end{lstlisting}
\end{frame}
@@ -157,7 +158,7 @@
\end{lstlisting}
\begin{itemize}
\item File with records like the one above is given
- \item Each record has fields separated by ;
+ \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
@@ -171,8 +172,8 @@
\begin{frame}[fragile]
\frametitle{Tokenization}
\begin{lstlisting}
- In[]: line = "parse this string"
- In[]: line.split()
+ In[]: line = "parse this string"
+ In[]: line.split()
\end{lstlisting}
\begin{itemize}
\item Original string is split on white-space (if no argument)
@@ -180,7 +181,7 @@
\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 = "A;015163;JOSEPH RAJ S;083;042;47;AA;72;244;;;"
In[]: record.split(';')
\end{lstlisting}
\end{frame}
@@ -207,9 +208,9 @@
\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[]: mark_str = "1.25"
+ In[]: mark = int(mark_str)
+ In[]: type(mark_str)
In[]: type(mark)
\end{lstlisting}
\begin{itemize}
@@ -220,20 +221,20 @@
\begin{frame}[fragile]
\frametitle{File parsing -- Solution}
\begin{lstlisting}
- In[]: math_B = [] # empty list to store marks
+ In[]: math_B = [] # empty list to store marks
In[]: for line in open("sslc1.txt"):
- ....: fields = line.split(";")
+ ....: fields = line.split(";")
- ....: reg_code = fields[0]
- ....: reg_code_clean = reg_code.strip()
+ ....: reg_code = fields[0]
+ ....: reg_code_clean = reg_code.strip()
- ....: math_mark_str = fields[5]
- ....: math_mark = float(math_mark_str)
+ ....: math_mark_str = fields[5]
+ ....: math_mark = float(math_mark_str)
- ....: if reg_code == "B":
- ....: math_B.append(math_mark)
+ ....: if reg_code == "B":
+ ....: math_B.append(math_mark)
- In[]: math_B_mean = sum(math_B) / len(math_B)
- In[]: math_B_mean
+ In[]: math_B_mean = sum(math_B) / len(math_B)
+ In[]: math_B_mean
\end{lstlisting}
\end{frame}