122 Out[]: ['hello', ' world'] # Note the whitespace before 'world' |
122 Out[]: ['hello', ' world'] # Note the whitespace before 'world' |
123 \end{lstlisting} |
123 \end{lstlisting} |
124 A string can be split based on the delimiter specified within quotes. A combination of more than one delimiter can also be used.\\ |
124 A string can be split based on the delimiter specified within quotes. A combination of more than one delimiter can also be used.\\ |
125 \typ{In []: greet.split(', ')}\\ |
125 \typ{In []: greet.split(', ')}\\ |
126 \typ{Out[]: ['hello', 'world']}\\Note the whitespace is not there anymore. |
126 \typ{Out[]: ['hello', 'world']}\\Note the whitespace is not there anymore. |
|
127 \newpage |
|
128 \section{Plotting from Files} |
|
129 \subsection{Opening files} |
127 |
130 |
128 \section{Plotting from Files} |
131 \typ{In []: f = open('datafile.txt')}\\By default opens in read mode. \\If file does not exist then it throws an exception\\ |
129 \subsection{Opening, reading and writing files} |
132 \typ{In []: f = open('datafile.txt','r')}\\Specifying the read mode\\ |
|
133 \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. |
|
134 |
|
135 \subsection{Reading from files} |
|
136 Just like lists files are iterable as well. |
130 |
137 |
131 \begin{lstlisting} |
138 \begin{lstlisting} |
132 In []: f = open('datafile.txt') #By default opens in read mode. If file does not exist then it throws an exception |
139 In []: for line in f: |
133 In []: f = open('datafile.txt','r') #Specifying the read mode |
140 ...: print line |
134 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. |
141 ...: |
|
142 ...: |
135 \end{lstlisting} |
143 \end{lstlisting} |
|
144 |
136 \subsection{Plotting} |
145 \subsection{Plotting} |
137 \begin{lstlisting} |
146 \begin{lstlisting} |
138 l = [] |
147 l = [] |
139 t = [] |
148 t = [] |
140 for line in open('pendulum.txt'): |
149 for line in open('pendulum.txt'): |