Difference between revisions of "Python-3.4.3/C2/Loading-Data-From-Files/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(3 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
|-
 
|-
 
| 00:01
 
| 00:01
| Hello Friends and Welcome to the spoken tutorial on '''"loading data from files".'''
+
| Hello friends and welcome to the '''spoken tutorial''' on '''"loading data from files".'''
  
 
|-
 
|-
 
| 00:07
 
| 00:07
| In this tutorial you will learn to,
+
| In this tutorial, you will learn to:
  
Read data from files, which contain data in:
+
read data from files which contain data in:
  
Single column format  
+
single '''column format'''
  
Or multiple columns separated by spaces or other delimiters
+
or multiple columns separated by '''space'''s or other '''delimiter'''s.
  
 
|-
 
|-
 
| 00:21
 
| 00:21
| To record this tutorial, I am using  
+
| To record this tutorial, I am using:
  
'''Ubuntu Linux 14.04''' operating system
+
'''Ubuntu Linux 14.04''' operating system,
  
'''Python 3.4.3'''
+
'''Python 3.4.3''',
  
'''IPython 5.1.0'''
+
'''IPython 5.1.0'''.
  
 
|-
 
|-
 
| 00:37
 
| 00:37
| You should know how to run basic '''Python''' commands on the '''ipython console.'''
+
| You should know how to '''run''' basic '''Python''' commands on the '''ipython console.'''
  
 
|-
 
|-
 
| 00:43
 
| 00:43
| If not, for relevant Python tutorials, please visit this website [http://spoken-tutorial.org/ http://spoken-tutorial.org]
+
| If not, for relevant '''Python tutorials''', please visit this website.
 +
[http://spoken-tutorial.org/ http://spoken-tutorial.org]
  
 
|-
 
|-
Line 50: Line 51:
 
|-
 
|-
 
| 01:22
 
| 01:22
| Type'' '''''cat'''''(space)'''''primes'''''(dot)'''''txt''''' ''
+
| Type: '' '''''cat'''''(space)'''''primes'''''(dot)'''''txt''''' ''  
  
 
|-
 
|-
 
| 01:29
 
| 01:29
| We can use the''' cat''' command to fetch data from the file and display it on the terminal.''press ''Enter
+
| We can use the''' cat''' command to fetch data from the file and display it on the terminal. Press '''Enter'''.
  
 
|-
 
|-
Line 66: Line 67:
 
|-
 
|-
 
| 01:50
 
| 01:50
| SoType'' '''''primes'''''(equal to)'''''loadtx'''''t(within parentheses)(within double quotes)'''''primes'''''(dot)'''''txt''''' and press''' ''Enter.'''
+
| So, type: '''primes'''''(equal to)'''''loadtxt'''''(within parentheses)(within double quotes)'''''primes'''''(dot)'''''txt''' and press '''Enter.'''
  
 
|-
 
|-
Line 78: Line 79:
 
|-
 
|-
 
| 02:18
 
| 02:18
| '''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'''.  
  
 
|-
 
|-
Line 86: Line 87:
 
|-
 
|-
 
| 02:29
 
| 02:29
| So, type, '''print''' (within '''parentheses''') '''primes''' and press '''Enter'''. We see the sequence printed.
+
| So, type: '''print''' (within parentheses) '''primes''' and press '''Enter'''. We see the sequence printed.
  
 
|-
 
|-
 
| 02:41
 
| 02:41
| We observe that all the numbers end with a '''period ‘.’ '''. This is because all these numbers are '''floats.'''
+
| We observe that all the numbers end with a period ‘.’ . This is because all these numbers are '''floats.'''
  
 
|-
 
|-
 
| 02:51
 
| 02:51
|Now Type '''cat'''''(space)'''''pendulum'''''(dot)'''''txt ''' and Press '''Enter'''.
+
|Now, type: '''cat'''''(space)'''''pendulum'''''(dot)'''''txt ''' and press '''Enter'''.
  
 
|-
 
|-
 
| 03:01
 
| 03:01
| This file contains two columns of data. This first column contains the length of the '''pendulum'''.
+
| This file contains two columns of data. This first column contains the length of the pendulum.
The second column contains the corresponding '''time period'''.
+
The second column contains the corresponding time period.
  
 
|-
 
|-
Line 107: Line 108:
 
|-
 
|-
 
| 03:23
 
| 03:23
| So Type '''pend'''''(equal to)'''''loadtxt'''''(within parentheses)(within double quotes)'''''pendulum'''''(dot)'''''txt''''' ''and'' press''' Enter.'''''
+
| So, type: '''pend'''''(equal to)'''''loadtxt'''''(within parentheses)(within double quotes)'''''pendulum'''''(dot)'''''txt''''' ''and press''' Enter.'''
  
 
|-  
 
|-  
Line 115: Line 116:
 
|-
 
|-
 
| 03:47
 
| 03:47
| Now print the variable''' pend''' to see what it contains.
+
| Now, print the variable''' pend''' to see what it contains.
Type'' '''''print'''''(within parentheses)'''''pend''''' ''and press''' Enter.'''
+
Type: '' '''''print'''''(within parentheses)'''''pend''''' ''and press''' Enter.'''
  
 
|-
 
|-
 
| 04:00
 
| 04:00
| Notice that''' '''variable has two sequences containing two columns of the data file.
+
| Notice that the variable has two sequences containing two columns of the data file.
  
 
|-
 
|-
Line 128: Line 129:
 
|-
 
|-
 
| 04:16
 
| 04:16
| So Type '''L'''(comma)'''T'''''(equal to)'''''loadtxt'''''(within parentheses within double quotes)'''''pendulum'''''(dot)'''''txt'''''(after double quotes comma)'''''unpack'''''(equal to)'''''True''''' ''
+
| So, type '''L'''(comma)'''T'''''(equal to)'''''loadtxt'''''(within parentheses within double quotes)'''''pendulum'''''(dot)'''''txt'''''(after double quotes comma)'''''unpack'''''(equal to)'''''True''''' ''
  
And press '''Enter.'''
+
and press '''Enter.'''
  
 
|-
 
|-
 
| 04:42
 
| 04:42
| Now print the variables''' L''' and''' T''', to see what they contain.
+
| Now print the variables''' L''' and''' T''' to see what they contain.
  
 
|-
 
|-
 
| 04:47
 
| 04:47
| Type'' '''''print'''''(within parentheses)'''''L''''' ''and press''' Enter'''
+
| Type: '' '''''print'''''(within parentheses)'''''L''''' ''and press''' Enter'''.
  
Type'' '''''print'''''(within parentheses)'''''T '''and press''' Enter'''
+
Type: '' '''''print'''''(within parentheses)'''''T '''and press''' Enter'''.
  
 
|-
 
|-
 
| 05:01
 
| 05:01
| Notice, that''' L''' and''' T''' now contain the first and second columns of data from the '''pendulum.txt '''respectively
+
| Notice, that''' L''' and''' T''' now contain the first and second columns of data from the '''pendulum.txt '''respectively.
  
 
|-
 
|-
Line 174: Line 175:
 
|-
 
|-
 
| 05:54
 
| 05:54
| So type '''cat''' space '''pendulum'''''(underscore)'''''semicolon'''''(dot)'''''txt''' Press ''' Enter''' We see the two columns separated by a '''semi-colon'''.
+
| So, type: '''cat''' space '''pendulum'''''(underscore)'''''semicolon'''''(dot)'''''txt''' and press ''' Enter'''. We see the two columns separated by a semicolon.
  
 
|-
 
|-
 
| 06:12
 
| 06:12
| Now, type'' '''''L'''''(comma)'''''T'''''(equal to)'''''loadtxt''''' (within parentheses within double quotes) '''''pendulum'''''(underscore)'''''semicolon''''' (dot)'''''txt'''''(after double quotes comma)'''''unpack'''''(equal to)'''''True'''''(comma)'''''delimiter'''''(equal to)(within double quotes)'''''semicolon'''''.''
+
| Now, type: '' '''''L'''''(comma)'''''T'''''(equal to)'''''loadtxt''''' (within parentheses within double quotes) '''''pendulum'''''(underscore)'''''semicolon''''' (dot)'''''txt'''''(after double quotes comma)'''''unpack'''''(equal to)'''''True'''''(comma)'''''delimiter'''''(equal to)(within double quotes)'''''semicolon'''''.''
  
 
And press '''Enter'''.
 
And press '''Enter'''.
Line 184: Line 185:
 
|-
 
|-
 
| 06:48
 
| 06:48
| Now'' '''''print'''''(within parentheses)'''''L''' and press '''Enter.'''
+
| Now, '''print'''''(within parentheses)'''''L''' and press '''Enter.'''
  
'''print'''''(within parentheses)'''''T '''''and press'' '''Enter. '''
+
'''print'''''(within parentheses)'''''T ''' and press '''Enter. '''
  
 
|-
 
|-
Line 194: Line 195:
 
|-
 
|-
 
| 07:09
 
| 07:09
| This brings us to the end of this tutorial. In this tutorial, we have learnt:
+
| This brings us to the end of this tutorial. In this tutorial, we have learnt to read data from files using the '''loadtxt()''' command.
 
+
To read data from files using the '''loadtxt()''' command.
+
  
 
|-
 
|-
 
| 07:20
 
| 07:20
| The data can be in  
+
| The data can be in:
  
A single column format Or multiple column format, separated by spaces or other delimiters.
+
a single column format or multiple column format, separated by '''space'''s or other '''delimiter'''s.
  
 
|-
 
|-
 
| 07:31
 
| 07:31
| Here are some self assessment questions for you to solve
+
| Here are some self assessment questions for you to solve.
  
'''loadtxt''' can read data only from a file with one column. Is it True or False?
+
1. '''loadtxt''' can read data only from a file with one column. Is it '''True''' or '''False'''?
 
+
Given a file''' data.txt''' with three columns of data separated by spaces.
+
  
 +
2. Given a file''' data.txt''' with three columns of data separated by spaces.
 
Read it into 3 separate simple sequences.
 
Read it into 3 separate simple sequences.
  
 
|-
 
|-
 
| 07:58
 
| 07:58
|Given a file''' data.txt''' with three columns of data separated by '''colon'''. Read it into 3 separate simple sequences.
+
|3. Given a file''' data.txt''' with three columns of data separated by '''colon'''. Read it into 3 separate simple sequences.
  
 
|-
 
|-
 
| 08:09
 
| 08:09
| Now let us look at the answers, The answer to the first question is False.  
+
| Now, let us look at the answers. The answer to the first question is '''False'''.  
  
 
|-
 
|-
Line 228: Line 226:
 
|-
 
|-
 
| 08:25
 
| 08:25
| The answer to the second question is,
+
| The answer to the second question is-
 
+
 
To separate data into three columns, we use the''' loadtxt()''' command as follows:
 
To separate data into three columns, we use the''' loadtxt()''' command as follows:
  
 
|-
 
|-
 
| 08:35
 
| 08:35
| '''x('''''equal to)'''''loadtxt'''''(within parentheses and within double quotes'')'''data'''''(dot'')''txt''(after double quotes ''comma'')'''unpack'''(equal to)'''True'''
+
| '''x('''''equal to)'''''loadtxt'''''(within parentheses and within double quotes'')'''data'''''(dot'')''txt''(after double quotes ''comma'')'''unpack'''(equal to)'''True'''.
  
 
|-
 
|-
 
| 08:50
 
| 08:50
| The answer to the third question is, We read into three separate sequences by using an additional argument of delimiter in the loadtxt command.
+
| The answer to the third question is-
 +
We read into three separate sequences by using an additional '''argument''' of '''delimiter''' in the '''loadtxt''' command.
  
 
|-
 
|-
 
| 09:03
 
| 09:03
| So '''x'''(''equal to'')'''loadtxt'''('' within parentheses, within double quotes'')'''data'''''(dot)'''''txt'''(after double quotes ''comma)'''''unpack'''''(equal to)'''''True'''''(comma)'''''delimiter'''''(equal to'')''(within double quotes)'''''colon'''
+
| So, '''x'''(''equal to'')'''loadtxt'''('' within parentheses, within double quotes'')'''data'''''(dot)'''''txt'''(after double quotes ''comma)'''''unpack'''''(equal to)'''''True'''''(comma)'''''delimiter'''''(equal to'')''(within double quotes)'''''colon'''.
  
 
|-
 
|-
 
| 09:22
 
| 09:22
|Do you have questions on THIS '''Spoken Tutorial?'''.Please visit this site.  
+
|Do you have questions on THIS '''Spoken Tutorial?'''. Please visit this site.  
  
 
|-
 
|-
Line 262: Line 260:
 
|-
 
|-
 
| 09:52
 
| 09:52
| The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India  
+
| The '''Spoken Tutorial''' project is funded by '''NMEICT, MHRD,''' Govt. of India.
  
 
|-
 
|-
 
| 09:59
 
| 09:59
| This is Prathamesh Salunke from IIT Bombay signing off. Thanks for watching.
+
| This is Prathamesh Salunke from '''IIT Bombay''', signing off. Thanks for watching.
  
 
|}
 
|}

Latest revision as of 16:43, 14 June 2019

Time Narration
00:01 Hello friends and welcome to the spoken tutorial on "loading data from files".
00:07 In this tutorial, you will learn to:

read data from files which contain data in:

single column format

or multiple columns separated by spaces or other delimiters.

00:21 To record this tutorial, I am using:

Ubuntu Linux 14.04 operating system,

Python 3.4.3,

IPython 5.1.0.

00:37 You should know how to run basic Python commands on the ipython console.
00:43 If not, for relevant Python tutorials, please visit this website.

http://spoken-tutorial.org

00:49 Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously. Now, type ipython3 and press Enter.
01:02 Let us initialise the pylab package.

Type percent pylab and press Enter.

01:12 Let us begin with reading the file primes.txt. This file contains a list of prime numbers listed in a column.
01:22 Type: cat(space)primes(dot)txt
01:29 We can use the cat command to fetch data from the file and display it on the terminal. Press Enter.
01:38 We see the prime numbers are displayed on the terminal.
01:43 Now we can use the loadtxt() command to store this list into the variable primes.
01:50 So, type: primes(equal to)loadtxt(within parentheses)(within double quotes)primes(dot)txt and press Enter.
02:07 Please make sure that you provide the correct path to the file, 'primes.txt'.
02:13 The file, in our case, is present in the home folder.
02:18 primes is now a sequence of prime numbers that was listed in the file primes.txt.
02:25 Now let us display the contents in the variable primes.
02:29 So, type: print (within parentheses) primes and press Enter. We see the sequence printed.
02:41 We observe that all the numbers end with a period ‘.’ . This is because all these numbers are floats.
02:51 Now, type: cat(space)pendulum(dot)txt and press Enter.
03:01 This file contains two columns of data. This first column contains the length of the pendulum.

The second column contains the corresponding time period.

03:15 Let us now read the data from the file into the variable pend using the loadtxt command.
03:23 So, type: pend(equal to)loadtxt(within parentheses)(within double quotes)pendulum(dot)txt and press Enter.
03:39 Please note that loadtxt needs both the columns of the file to have equal number of rows.
03:47 Now, print the variable pend to see what it contains.

Type: print(within parentheses)pend and press Enter.

04:00 Notice that the variable has two sequences containing two columns of the data file.
04:07 Let us use an additional argument of the loadtxt command to read the data into two separate sequences.
04:16 So, type L(comma)T(equal to)loadtxt(within parentheses within double quotes)pendulum(dot)txt(after double quotes comma)unpack(equal to)True

and press Enter.

04:42 Now print the variables L and T to see what they contain.
04:47 Type: print(within parentheses)L and press Enter.

Type: print(within parentheses)T and press Enter.

05:01 Notice, that L and T now contain the first and second columns of data from the pendulum.txt respectively.
05:12 unpack(equal to)True has made the two columns into two separate and simple sequences.
05:20 Pause the video over here and try out the following exercise and resume the video.
05:27 Read the data from the file pendulum(underscore)semicolon(dot)txt.
05:33 This file contains data in two columns. These columns are separated by semicolons.

Use the IPython help to see how to do this.

05:45 Let us look at the solution. Switch to the terminal.
05:50 First we will see the content of the file.
05:54 So, type: cat space pendulum(underscore)semicolon(dot)txt and press Enter. We see the two columns separated by a semicolon.
06:12 Now, type: L(comma)T(equal to)loadtxt (within parentheses within double quotes) pendulum(underscore)semicolon (dot)txt(after double quotes comma)unpack(equal to)True(comma)delimiter(equal to)(within double quotes)semicolon.

And press Enter.

06:48 Now, print(within parentheses)L and press Enter.

print(within parentheses)T and press Enter.

07:03 This will display the contents inside the two variables L and T.
07:09 This brings us to the end of this tutorial. In this tutorial, we have learnt to read data from files using the loadtxt() command.
07:20 The data can be in:

a single column format or multiple column format, separated by spaces or other delimiters.

07:31 Here are some self assessment questions for you to solve.

1. loadtxt can read data only from a file with one column. Is it True or False?

2. Given a file data.txt with three columns of data separated by spaces. Read it into 3 separate simple sequences.

07:58 3. Given a file data.txt with three columns of data separated by colon. Read it into 3 separate simple sequences.
08:09 Now, let us look at the answers. The answer to the first question is False.
08:17 The loadtxt() command can read data from files having single columns as well as multiple columns.
08:25 The answer to the second question is-

To separate data into three columns, we use the loadtxt() command as follows:

08:35 x(equal to)loadtxt(within parentheses and within double quotes)data(dot)txt(after double quotes comma)unpack(equal to)True.
08:50 The answer to the third question is-

We read into three separate sequences by using an additional argument of delimiter in the loadtxt command.

09:03 So, x(equal to)loadtxt( within parentheses, within double quotes)data(dot)txt(after double quotes comma)unpack(equal to)True(comma)delimiter(equal to)(within double quotes)colon.
09:22 Do you have questions on THIS Spoken Tutorial?. Please visit this site.
09:29 Do you have any general / technical questions? Please visit the forum given in the link.
09:37 The FOSSEE team coordinates coding of solved examples of popular books.
09:43 We give honorarium and certificates for those who do this. For more details, please visit this site.
09:52 The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India.
09:59 This is Prathamesh Salunke from IIT Bombay, signing off. Thanks for watching.

Contributors and Content Editors

PoojaMoolya, Pratik kamble, Sandhya.np14