# HG changeset patch # User Santosh G. Vattam # Date 1258552608 -19800 # Node ID c9f05808e1c498eed71915cc188cf4ca1a1cbe77 # Parent f5bcb974a6652f8afddc4b65ba7dab8827e3914c Updated cheatsheet of session 2 day 1. diff -r f5bcb974a665 -r c9f05808e1c4 day1/cheatsheet2.tex --- a/day1/cheatsheet2.tex Wed Nov 18 18:37:15 2009 +0530 +++ b/day1/cheatsheet2.tex Wed Nov 18 19:26:48 2009 +0530 @@ -124,15 +124,24 @@ A string can be split based on the delimiter specified within quotes. A combination of more than one delimiter can also be used.\\ \typ{In []: greet.split(', ')}\\ \typ{Out[]: ['hello', 'world']}\\Note the whitespace is not there anymore. - +\newpage \section{Plotting from Files} -\subsection{Opening, reading and writing files} +\subsection{Opening files} + +\typ{In []: f = open('datafile.txt')}\\By default opens in read mode. \\If file does not exist then it throws an exception\\ +\typ{In []: f = open('datafile.txt','r')}\\Specifying the read mode\\ +\typ{In []: f = open('datafile.txt', 'w')}\\Opens the file in write mode. \\If the file already exists, then it deletes all the previous content and opens. + +\subsection{Reading from files} +Just like lists files are iterable as well. \begin{lstlisting} - In []: f = open('datafile.txt') #By default opens in read mode. If file does not exist then it throws an exception - In []: f = open('datafile.txt','r') #Specifying the read mode - In []: f = open('datafile.txt', 'w') #Opens the file in write mode. If the file already exists, then it deletes all the previous content and opens. + In []: for line in f: + ...: print line + ...: + ...: \end{lstlisting} + \subsection{Plotting} \begin{lstlisting} l = []