Difference between revisions of "Python/C2/loading-data-from-files/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
(One intermediate revision by the same user not shown) | |||
Line 9: | Line 9: | ||
|- | |- | ||
| 00:06 | | 00:06 | ||
− | | At the end of this tutorial, you will be able to, | + | | At the end of this tutorial, you will be able to, Read data from files, containing a single column of data |
− | + | Read multiple columns of data, separated by spaces or other delimiters. | |
− | + | ||
|- | |- | ||
Line 124: | Line 123: | ||
|- | |- | ||
|04:29 | |04:29 | ||
− | |So type print space L | + | |So type print space L, print space T |
− | print space T | + | |
|- | |- | ||
Line 158: | Line 156: | ||
|05:34 | |05:34 | ||
|Switch back to the terminal | |Switch back to the terminal | ||
− | + | L comma T = loadtxt within bracket in single quote slash home slash fossee slash pendulum underscore semicolon.txt then comma unpack=True comma delimiter=semi-colon within single quote | |
− | L comma T = loadtxt within bracket in single quote slash home slash fossee slash pendulum underscore semicolon.txt comma unpack=True comma | + | |
− | + | ||
|- | |- | ||
− | |06: | + | |06:33 |
|Okay then type print L | |Okay then type print L | ||
Latest revision as of 10:44, 27 March 2017
Time | Narration |
00:01 | Hello Friends and Welcome to this tutorial on "loading data from files". |
00:06 | At the end of this tutorial, you will be able to, Read data from files, containing a single column of data
Read multiple columns of data, separated by spaces or other delimiters. |
00:19 | Let us switch to the terminal and start IPython, using ipython hypen pylab |
00: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. |
00:45 | Please make sure that you provide the correct path of the file, 'primes.txt'. |
00:50 | The file, in our case, is presented in slash home slash fossee slash primes.txt. |
00:59 | Otherwise we can use the cat command to locate the file and read the contents of it. |
01:05 | So type cat slash home slash fossee slash primes.txt |
01:15 | Now let us read this list into the variable primes. |
01:20 | So type primes = loadtxt within bracket in single quotes slash home slash fossee slash primes.txt |
01:41 | primes is now a sequence of prime numbers, that was listed in the file,``primes.txt``. |
01:49 | We now type primes,print primes to see the sequence printed so type print space primes. |
02:00 | We observe that all the numbers end with a period. |
02:04 | This is so, because these numbers are actually read as floats |
02:10 | Now, let us use the loadtxt command to read a file pendulum.txt that contains two columns of data. |
02:19 | This file contains the length of the pendulum in the first column and the corresponding time period in the second. |
02:26 | Note that here loadtxt needs both the columns to have equal number of rows. |
02:31 | We can use the cat command to view the contents of this file. |
02:36 | So type cat slash home slash fossee slash pendulum.txt |
02:50 | Let us, now, read the data into the variable pend. |
02:55 | Again, it is assumed that the file is in slash home slash fossee |
03:02 | So type pend = loadtxt within bracket in single quote slash home slash fossee slash pendulum.txt |
03:21 | Let us now print the variable pend and see what it contains. |
03:26 | So type print pend |
03:31 | Notice that pend is not a simple sequence like primes . |
03:35 | It has two sequences, containing both the columns of the data file. |
03:40 | Let us use an additional argument of the load txt command, to read it into two separate, simple sequences. |
03:50 | So type L, T space = space load txt within bracket single quote slash home slash fossee slash pendulum.txt comma unpack=True |
04:23 | Let us now, print the variables L and T, to see what they contain. |
04:29 | So type print space L, print space T |
04: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. |
04:50 | unpack=True has given us the two columns into two separate sequences instead of one complex sequence. |
05:00 | Till now, we have learnt the basic use of the load txt command. |
05:05 | Let us try an example. |
05:07 | Pause the video here, try out the following exercise and resume the video. |
05: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. |
05:27 | Use the IPython help to see how to do this. |
05:34 | Switch back to the terminal
L comma T = loadtxt within bracket in single quote slash home slash fossee slash pendulum underscore semicolon.txt then comma unpack=True comma delimiter=semi-colon within single quote |
06:33 | Okay then type print L |
06:40 | print T |
06:45 | This brings us to the end of this tutorial. |
06:48 | In this tutorial, we have learnt to, |
06:51 | 1. To Read data from files, containing a single column of data using the loadtxt command. |
06:58 | 2. To Read multiple columns of data, separated by spaces or other delimiters. |
07: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? |
07:18 | 2. Given a file data.txt with three columns of data separated by spaces, read it into 3 separate simple sequences. |
07:29 | 3. Given a file data.txt with three columns of data separated by ":", read it into 3 separate simple sequences. |
07:45 | And now the answers,1. False. |
07:50 | loadtxt command can read data from files having both single columns as well as multiple columns. |
07: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 |
08: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 semicolon) |
08:51 | Hope you have enjoyed this tutorial and found it useful. |
08:55 | Thank you! |