Difference between revisions of "Python/C2/loading-data-from-files/English-timed"
From Script | Spoken-Tutorial
(Created page with '{| border=1 !Visual Cue !Narration |- | 0:01 | Hello Friends and Welcome to this tutorial on "loading data from files". |- | 0:06 | At the end of this tutorial, you will be able…') |
|||
| Line 27: | Line 27: | ||
|- | |- | ||
| 0:50 | | 0:50 | ||
| − | |The file, in our case, is present in | + | |The file, in our case, is present in slash home slash fossee slash primes.txt. |
|- | |- | ||
| 0:59 | | 0:59 | ||
| − | | Otherwise we can use the | + | | Otherwise we can use the cat command to locate the file and read the contents of it. |
|- | |- | ||
| Line 39: | Line 39: | ||
|- | |- | ||
| 1:15 | | 1:15 | ||
| − | | Now let us read this list into the variable | + | | Now let us read this list into the variable primes. |
|- | |- | ||
| Line 47: | Line 47: | ||
|- | |- | ||
| 1:41 | | 1:41 | ||
| − | | | + | |primes is now a sequence of prime numbers, that was listed in the file,``primes.txt``. |
|- | |- | ||
| 1:49 | | 1:49 | ||
| − | |We now type, | + | |We now type,print space primes to see the sequence printed. |
|- | |- | ||
| Line 59: | Line 59: | ||
|- | |- | ||
| 2:04 | | 2:04 | ||
| − | |This is so, because these numbers are actually read as | + | |This is so, because these numbers are actually read as floats |
|- | |- | ||
| 2:10 | | 2:10 | ||
| − | | Now, let us use the | + | | Now, let us use the loadtxt command to read a file pendulum.txt that contains two columns of data. |
|- | |- | ||
| Line 71: | Line 71: | ||
|- | |- | ||
| 2:26 | | 2:26 | ||
| − | | Note that here | + | | Note that here loadtxt needs both the columns to have equal number of rows. |
|- | |- | ||
| 2:31 | | 2:31 | ||
| − | |We use the | + | |We use the cat command to view the contents of this file. |
|- | |- | ||
| Line 83: | Line 83: | ||
|- | |- | ||
| 2:50 | | 2:50 | ||
| − | | Let us, now, read the data into the variable | + | | Let us, now, read the data into the variable pend. |
|- | |- | ||
| 2:55 | | 2:55 | ||
| − | |Again, it is assumed that the file is in | + | |Again, it is assumed that the file is in slash home slash fossee |
|- | |- | ||
| Line 95: | Line 95: | ||
|- | |- | ||
| 3:21 | | 3:21 | ||
| − | | Let us now print the variable | + | | Let us now print the variable pend and see what it contains. |
|- | |- | ||
| Line 103: | Line 103: | ||
|- | |- | ||
| 3:31 | | 3:31 | ||
| − | | Notice that | + | | Notice that pend is not a simple sequence like primes . |
|- | |- | ||
| Line 111: | Line 111: | ||
|- | |- | ||
| 3:40 | | 3:40 | ||
| − | | Let us use an additional argument of the | + | | Let us use an additional argument of the loadtxt command, to read it into two separate, simple sequences. |
|- | |- | ||
| Line 128: | Line 128: | ||
|- | |- | ||
| 4:39 | | 4:39 | ||
| − | | Notice, that L and T now contain the first and second columns of data from the data file, | + | | 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. |
|- | |- | ||
| 4:50 | | 4:50 | ||
| − | | | + | |unpack=True has given us the two columns into two separate sequences instead of one complex sequence. |
|- | |- | ||
| 5:00 | | 5:00 | ||
| − | | Till now, we have learnt the basic use of the | + | | Till now, we have learnt the basic use of the loadtxt command. |
|- | |- | ||
| Line 148: | Line 148: | ||
|- | |- | ||
| 5:12 | | 5:12 | ||
| − | |Read the file | + | |Read the file pendulum underscore semicolon.txt which contains the same data as pendulum.txt, but the columns are separated by semi-colons instead of spaces. |
|- | |- | ||
| Line 188: | Line 188: | ||
| 7:04 | | 7:04 | ||
| Here are some self assessment questions for you to solve | | Here are some self assessment questions for you to solve | ||
| − | 1. | + | 1. loadtxt can read data from a file with one column only. True or False? |
|- | |- | ||
| 7:18 | | 7:18 | ||
| − | | 2. Given a file | + | | 2. Given a file data.txt with three columns of data separated by spaces, read it into 3 separate simple sequences. |
|- | |- | ||
| 7:29 | | 7:29 | ||
| − | | 3. Given a file | + | | 3. Given a file data.txt with three columns of data separated by ":", read it into 3 separate simple sequences. |
|- | |- | ||
| Line 204: | Line 204: | ||
|- | |- | ||
| 7:50 | | 7:50 | ||
| − | | | + | |loadtxt command can read data from files having both single columns as well as multiple columns. |
|- | |- | ||
Revision as of 17:09, 8 March 2013
| Visual Cue | Narration |
|---|---|
| 0:01 | Hello Friends and Welcome to this tutorial on "loading data from files". |
| 0:06 | At the end of this tutorial, you will be able to,
|
| 0:19 | Let us switch to the terminal and start IPython, using ipython hypen pylab |
| 0:33 | 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. |
| 0:45 | Please make sure that you provide the correct path of the file, 'primes.txt'. |
| 0:50 | The file, in our case, is present in slash home slash fossee slash primes.txt. |
| 0:59 | Otherwise we can use the cat command to locate the file and read the contents of it. |
| 1:05 | So type cat slash home slash fossee slash primes.txt |
| 1:15 | Now let us read this list into the variable primes. |
| 1:20 | So type primes = loadtxt within bracket in single quotes slash home slash fossee slash primes.txt |
| 1:41 | primes is now a sequence of prime numbers, that was listed in the file,``primes.txt``. |
| 1:49 | We now type,print space primes to see the sequence printed. |
| 2:00 | We observe that all the numbers end with a period. |
| 2:04 | This is so, because these numbers are actually read as floats |
| 2:10 | Now, let us use the loadtxt command to read a file pendulum.txt that contains two columns of data. |
| 2:19 | This file contains the length of the pendulum in the first column and the corresponding time period in the second. |
| 2:26 | Note that here loadtxt needs both the columns to have equal number of rows. |
| 2:31 | We use the cat command to view the contents of this file. |
| 2:36 | So type cat slash home slash fossee slash pendulum.txt |
| 2:50 | Let us, now, read the data into the variable pend. |
| 2:55 | Again, it is assumed that the file is in slash home slash fossee |
| 3:02 | So type pend = loadtxt within bracket in single quote slash home slash fossee slash pendulum.txt |
| 3:21 | Let us now print the variable pend and see what it contains. |
| 3:26 | So type print pend |
| 3:31 | Notice that pend is not a simple sequence like primes . |
| 3:35 | It has two sequences, containing both the columns of the data file. |
| 3:40 | Let us use an additional argument of the loadtxt command, to read it into two separate, simple sequences. |
| 3:50 | So type L, T space = space loadtxt within bracket single quoteslash home slash fossee slash pendulum.txt comma unpack=True |
| 4:23 | Let us now, print the variables L and T, to see what they contain. |
| 4:29 | So type print space L
print space T |
| 4:39 | 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. |
| 4:50 | unpack=True has given us the two columns into two separate sequences instead of one complex sequence. |
| 5:00 | Till now, we have learnt the basic use of the loadtxt command. |
| 5:05 | Let us try an example. |
| 5:07 | Pause the video here, try out the following exercise and resume the video. |
| 5:12 | Read the file pendulum underscore semicolon.txt which contains the same data as pendulum.txt, but the columns are separated by semi-colons instead of spaces. |
| 5:27 | Use the IPython help to see how to do this. |
| 5:34 | Switch back to the terminal
L comma T = loadtxt within bracket in single quote slash home slash fossee slash pendulum underscore semicolon.txt comma unpack=True comma delimiter=semi-colon within single quote |
| 6:54 | Okay then type print L |
| 6:40 | print T |
| 6:45 | This brings us to the end of this tutorial. |
| 6:48 | In this tutorial, we have learnt to, |
| 6:51 | 1. To Read data from files, containing a single column of data using the loadtxt command. |
| 6:58 | 2. To Read multiple columns of data, separated by spaces or other delimiters. |
| 7:04 | 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? |
| 7:18 | 2. Given a file data.txt with three columns of data separated by spaces, read it into 3 separate simple sequences. |
| 7:29 | 3. Given a file data.txt with three columns of data separated by ":", read it into 3 separate simple sequences. |
| 7:45 | And the answers,1. False. |
| 7:50 | loadtxt command can read data from files having both single columns as well as multiple columns. |
| 7:58 | 2. A file with three columns of data separated by spaces to be read into 3 separate sequences, we use the loadtxt command as, x = loadtxt within bracket in double quotes data.txt comma unpack=True |
| 8:19 | 3. 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
x = loadtxt within bracket in double quotes data.txt comma unpack=True comma delimiter=in double quotes colon) |
| 8:51 | Hope you have enjoyed this tutorial and found it useful. |
| 8:55 | Thank you! |