| Show Slide 1
Containing title, name of the production team along with the logo of MHRD
|Hello Friends and Welcome to this tutorial on "loading data from files".|
| Show Slide 2
| At the end of this tutorial, you will be able to,
| Switch to the terminal
|Let us switch to the terminal and start IPython, using ipython -pylab|
|Navigate to the path in the OS, open the file and show it||Now, Let us begin with reading the file primes.txt, which contains a list of prime numbers listed in a column, using the loadtxt command. Please make sure that you provide the correct path of the file, 'primes.txt'. The file, in our case, is present in /home/fossee/primes.txt.|
|cat /home/fossee/primes.txt||Otherwise we can use the cat command to locate the file and read the contents of it.|
|primes = loadtxt('/home/fossee/primes.txt')||Now let us read this list into the variable primes.|
|print primes|| primes is now a sequence of prime numbers, that was listed in the file,``primes.txt``.
We now type, print primes to see the sequence printed.
|Highlight the output on the terminal||We observe that all the numbers end with a period. This is so, because these numbers are actually read as floats.|
|cat /home/fossee/pendulum.txt|| Now, let us use the loadtxt command to read a file pendulum.txt that contains two columns of data. This file contains the length of the pendulum in the first column and the corresponding time period in the second. Note that here loadtxt needs both the columns to have equal number of rows.
We use the cat command to view the contents of this file.
|pend = loadtxt('/home/fossee/pendulum.txt')||Let us, now, read the data into the variable pend. Again, it is assumed that the file is in /home/fossee/|
|print pend||Let us now print the variable pend and see what it contains.|
|L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True)||Notice that pend is not a simple sequence like primes. It has two sequences, containing both the columns of the data file. Let us use an additional argument of the loadtxt command, to read it into two separate, simple sequences.|
| print L
|Let us now, print the variables L and T, to see what they contain.|
|Notice, that L and T now contain the first and second columns of data from the data file, pendulum.txt, and they are both simple sequences. unpack=True has given us the two columns into two separate sequences instead of one complex sequence.|
| Show Slide 3
| Till now, we have learnt the basic use of the loadtxt command. Let us try an example.
Pause the video here, try out the following exercise and resume the video.
Read the file pendulum_semicolon.txt which contains the same data as pendulum.txt, but the columns are separated by semi-colons instead of spaces. Use the IPython help to see how to do this.
| Switch back to the terminal
L, T = loadtxt('/home/fossee/pendulum_semicolon.txt', unpack=True, delimiter=';') print L print T
| Show Slide 4
| This brings us to the end of this tutorial. In this tutorial, we have learnt to,
| Show Slide 5
|| Here are some self assessment questions for you to solve
| Show Slide 6
Solution of self assessment questions on slide
| And the answers,
Enumerated list ends without a blank line; unexpected unindent.
x = loadtxt("data.txt", unpack=True)
Enumerated list ends without a blank line; unexpected unindented.
x = loadtxt("data.txt", unpack=True, delimiter=":")
| Show Slide 7
|Hope you have enjoyed this tutorial and found it useful. Thank you!|