Difference between revisions of "Python/C2/loading-data-from-files/English-timed"

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

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!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha