Python/C2/loading-data-from-files/English

From Script | Spoken-Tutorial
Revision as of 11:27, 29 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
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

Learning objectives

At the end of this tutorial, you will be able to,
  1. Read data from files, containing a single column of data
  2. Read multiple columns of data, separated by spaces or other delimiters.


Switch to the terminal
ipython -pylab
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
print T
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

Assignment 1

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

Summary slide

This brings us to the end of this tutorial. In this tutorial, we have learnt to,
  1. To Read data from files, containing a single column of data using the loadtxt command.
  2. To Read multiple columns of data, separated by spaces or other delimiters.


Show Slide 5


Self assessment questions slide

Here are some self assessment questions for you to solve
  1. loadtxt can read data from a file with one column only. True or False?
  2. Given a file data.txt with three columns of data separated by spaces, read it into 3 separate simple sequences.
  3. Given a file data.txt with three columns of data separated by ":", read it into 3 separate simple sequences.


Show Slide 6

Solution of self assessment questions on slide

And the answers,
  1. False. loadtxt command can read data from files having both single columns as well as multiple columns.
  2. A file with three columns of data separated by spaces to be read into 3 separate sequences, we use the loadtxt command as,

Enumerated list ends without a blank line; unexpected unindent.

x = loadtxt("data.txt", unpack=True)
  1. If a file with three columns of data separated by delimiters,we read it into three separate sequences by using an additional argument of delimiter in the loadtxt command

Enumerated list ends without a blank line; unexpected unindented.

x = loadtxt("data.txt", unpack=True, delimiter=":")
Show Slide 7

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika