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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "Loading data from files - English '''Title of script: Loading data files from files''' '''Author: Ankit''' '''Keywords: video tutorial, read data, files, loadtxt, ipython'...")
 
 
(One intermediate revision by one other user not shown)
Line 7: Line 7:
  
 
'''Keywords: video tutorial, read data, files, loadtxt, ipython'''
 
'''Keywords: video tutorial, read data, files, loadtxt, ipython'''
 
  
  
Line 30: Line 29:
  
 
* Multiple columns separated by spaces or other delimiters
 
* Multiple columns separated by spaces or other delimiters
 
 
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| In this tutorial you will learn to,
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| 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 spaces or other delimiters
+
 
+
 
+
  
 
|-
 
|-
Line 50: Line 41:
  
 
* '''Ubuntu Linux 14.04'''
 
* '''Ubuntu Linux 14.04'''
 
  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| To record this tutorial, I am using  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| To record this tutorial, I am using  
Line 57: Line 47:
 
* '''Python 3.4.3'''
 
* '''Python 3.4.3'''
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 65: Line 53:
 
Pre-requisites
 
Pre-requisites
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| You should know how to run basic '''Python''' commands on the '''ipython console.'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| You should know how to run basic '''Python''' commands on the '''ipython console.'''
 
  
 
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 76: Line 60:
  
 
'''ipython3'''
 
'''ipython3'''
 
 
  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously.  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Let us first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously.  
 
  
 
Now, type '''ipython3''' and press '''Enter'''.  
 
Now, type '''ipython3''' and press '''Enter'''.  
Line 90: Line 71:
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Let us initialise the '''pylab''' package.
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Let us initialise the '''pylab''' package.
  
 
+
Type '''percent pylab '''and press''' Enter.'''
Type '''%pylab '''and press''' Enter.'''
+
  
 
|-
 
|-
Line 106: Line 86:
 
and press Enter
 
and press Enter
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Let us begin with reading the file''' primes.txt'''. This file contains a list of prime numbers listed in a column.
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Let us begin with reading the file''' primes.txt'''. This file contains a list of prime numbers listed in a column.
 
  
 
Type'' '''''cat'''''(space)'''''primes'''''(dot)'''''txt''''' ''
 
Type'' '''''cat'''''(space)'''''primes'''''(dot)'''''txt''''' ''
Line 130: Line 109:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Please make sure that you provide the correct path to the file, ''''primes.txt'.'''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Please make sure that you provide the correct path to the file, ''''primes.txt'.'''
 
  
 
The file, in our case, is present in the '''home''' folder.
 
The file, in our case, is present in the '''home''' folder.
Line 141: Line 119:
 
'''print(primes)''' and press '''Enter'''
 
'''print(primes)''' and press '''Enter'''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| '''primes''' is now a sequence of prime numbers, that was listed in the file, '''primes.txt'''.  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| '''primes''' is now a sequence of prime numbers, that was listed in the file, '''primes.txt'''.  
 
  
 
Now let us display the contents in the variable '''primes.'''
 
Now let us display the contents in the variable '''primes.'''
  
 
+
So, type, '''print''' (within '''parentheses''') '''primes''' and press '''Enter'''.
'''So,''' type,'' '''''print'''''(within parentheses)'''''primes''''' ''to
+
 
+
  
 
We see the sequence printed.
 
We see the sequence printed.
Line 157: Line 132:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| We observe that all the numbers end with a period '''‘.’'''.  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| We observe that all the numbers end with a period '''‘.’'''.  
  
 
+
This is because all these numbers are '''floats.'''
This is because these numbers are '''floats.'''
+
 
+
 
+
 
+
  
 
|-
 
|-
Line 169: Line 140:
  
 
'''cat pendulum.txt'''
 
'''cat pendulum.txt'''
 
  
 
'''Highlight Cat'''
 
'''Highlight Cat'''
 
  
 
and press''' Enter'''
 
and press''' Enter'''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Type '''cat'''''(space)'''''pendulum'''''(dot)'''''txt '''
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Type '''cat'''''(space)'''''pendulum'''''(dot)'''''txt '''
  
 
+
Press '''Enter'''.
Press Enter.
+
 
+
  
 
This file contains two columns of data.
 
This file contains two columns of data.
  
 +
This first column contains the length of the '''pendulum'''.
  
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 199: Line 162:
  
 
and press Enter
 
and press Enter
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Let us, now, read the data from the file into the variable''' pend''' using the''' loadtxt''' command.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Let us now read the data from the file into the variable''' pend''' using the''' loadtxt''' command.
 
+
  
 
Type '''pend'''''(equal to)'''''loadtxt'''''(within parentheses)(within double quotes)'''''pendulum'''''(dot)'''''txt''''' ''and'' press''' Enter.'''''
 
Type '''pend'''''(equal to)'''''loadtxt'''''(within parentheses)(within double quotes)'''''pendulum'''''(dot)'''''txt''''' ''and'' press''' Enter.'''''
Line 207: Line 169:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"|  
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Please note that '''loadtxt''' needs both the columns of the file to have equal number of rows.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Please note that '''loadtxt''' needs both the columns of the file to have equal number of rows.
 
 
 
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| [IPython Terminal]
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| [IPython Terminal]
 
  
 
print'''(pend)'''
 
print'''(pend)'''
  
 
and press '''Enter'''
 
and press '''Enter'''
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Now print the variable''' pend''' and to see what it contains.
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| 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.'''
Line 225: Line 182:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| [IPython Terminal]
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| [IPython Terminal]
 
  
 
Type '''L''','''T'''<nowiki>=</nowiki>'''loadtxt(“pendulum.txt”, unpack=True)'''
 
Type '''L''','''T'''<nowiki>=</nowiki>'''loadtxt(“pendulum.txt”, unpack=True)'''
 
  
 
and press Enter
 
and press Enter
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Notice that''' '''variable has two sequences containing two columns of the data file.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Notice that''' '''variable has two sequences containing two columns of the data file.
 
  
 
Let us use an additional argument of the''' loadtxt''' command to read the data into two separate sequences.
 
Let us use an additional argument of the''' loadtxt''' command to read the data into two separate sequences.
  
 +
Type '''L'''(comma)'''T'''''(equal to)'''''loadtxt'''''(within parentheses within double quotes)'''''pendulum'''''(dot)'''''txt'''''(after double quotes comma)'''''unpack'''''(equal to)'''''True''''' ''
  
Type '''L'''(comma)'''T'''''(equal to)'''''loadtxt'''''(within parentheses)(inside double quotes)'''''pendulum'''''(dot)'''''txt'''''(after double quotes comma)'''''unpack'''''(equal to)'''''True''''' ''and press''''' Enter'''''
+
And press '''Enter.'''
  
 
|-
 
|-
Line 248: Line 203:
 
and press Enter
 
and press Enter
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Now print the variables''' L''' and''' T''', to see what they contain.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Now print the variables''' L''' and''' T''', to see what they contain.
 
  
 
Type'' '''''print'''''(within parentheses)'''''L''''' ''and press''' Enter'''
 
Type'' '''''print'''''(within parentheses)'''''L''''' ''and press''' Enter'''
Line 257: Line 211:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Highlight the output
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Highlight the output
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Notice, that''' L''' and''' T''' now contain the first and second columns of data from the '''pendulum.txt '''respectively
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Notice, that''' L''' and''' T''' now contain the first and second columns of data from the '''pendulum.txt '''respectively
 
  
 
'''unpack(equal to)True''' has made the two columns into two separate and simple sequences.
 
'''unpack(equal to)True''' has made the two columns into two separate and simple sequences.
Line 267: Line 220:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Pause the video over here and try out the following exercise and resume the video.
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Pause the video over here and try out the following exercise and resume the video.
  
 +
*Read the data from the file''' pendulum'''''(underscore)'''''semicolon'''''(dot)'''''txt. '''
  
Read the data from the file''' pendulum'''''(underscore)'''''semicolon'''''(dot)'''''txt. '''
+
*This file contains data in two columns. These columns are separated by semicolons.
  
This file contains data in two columns. These columns are separated by semicolons.
+
*Use the''' IPython''' help to see how to do this.
 
+
Use the''' IPython''' help to see how to do this.
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| [IPython Terminal]
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| [IPython Terminal]
 
  
 
Type
 
Type
  
 
'''L, T = loadtxt(“pendulum_semicolon.txt”, unpack=True,delimiter<nowiki>=”;”</nowiki>)'''
 
'''L, T = loadtxt(“pendulum_semicolon.txt”, unpack=True,delimiter<nowiki>=”;”</nowiki>)'''
 
  
  
Line 288: Line 238:
 
First we will see the content of the file.
 
First we will see the content of the file.
  
So type cat '''pendulum'''''(underscore)'''''semicolon'''''(dot)'''''txt'''
+
So type '''cat''' space '''pendulum'''''(underscore)'''''semicolon'''''(dot)'''''txt'''
 
+
''Press''''' Enter'''
+
  
We see the two columns separated by a semi-colon.
+
Press ''' Enter'''
  
 +
We see the two columns separated by a '''semi-colon'''.
  
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 306: Line 255:
 
print'''(L)'''
 
print'''(L)'''
  
and press Enter
+
and press '''Enter'''
  
 
[IPython Terminal]
 
[IPython Terminal]
Line 331: Line 280:
 
The data can be in  
 
The data can be in  
  
# A single column format
+
* A single column format
 
+
or
+
 
+
# Multiple column format, separated by spaces or other delimiters.
+
 
+
  
 +
* Or multiple column format, separated by spaces or other delimiters.
  
 
|-
 
|-
Line 343: Line 288:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Here are some self assessment questions for you to solve
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| 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?
+
# '''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.
 
# Given a file''' data.txt''' with three columns of data separated by spaces.
  
Line 350: Line 295:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Evaluation slide
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Evaluation slide
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| # Given a file''' data.txt''' with three columns of data separated by ":"
+
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"|
 +
# Given a file''' data.txt''' with three columns of data separated by '''colon'''.
  
 
Read it into 3 separate simple sequences.
 
Read it into 3 separate simple sequences.
Line 359: Line 305:
 
Solution 1:
 
Solution 1:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Now let us look at the answers,
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| Now let us look at the answers,
 
  
 
The answer to the first question is False.  
 
The answer to the first question is False.  
 
  
 
The '''loadtxt() '''command can read data from files having single columns as well as multiple columns.
 
The '''loadtxt() '''command can read data from files having single columns as well as multiple columns.
Line 371: Line 315:
 
Solution 2:  
 
Solution 2:  
  
 +
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| The answer to the second question is,
  
 +
To separate data into three columns, we use the''' loadtxt()''' command as follows:
  
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| To separate data into three columns, we use the''' loadtxt()''' command as follows:
+
'''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 double qu otes'')'''data'''''(dot'')''txt''(after double quotes ''comma'')'''unpack'''(equal to)'''True'''
+
  
 
|-
 
|-
Line 384: Line 327:
 
Solution 3:
 
Solution 3:
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| The answer to the third question is,
 
| style="background-color:#ffffff;border-top:none;border-bottom:1pt solid #000001;border-left:1pt solid #000001;border-right:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.032cm;padding-right:0.191cm;"| The answer to the third question is,
 
  
 
We read into three separate sequences by using an additional argument of delimiter in the loadtxt command.
 
We read into three separate sequences by using an additional argument of delimiter in the loadtxt command.
 
  
 
'''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)'''''semicolon'''
 
'''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)'''''semicolon'''

Latest revision as of 15:05, 7 September 2017

Loading data from files - English


Title of script: Loading data files from files

Author: Ankit

Keywords: video tutorial, read data, files, loadtxt, ipython


Visual Cue
Narration
Show Slide

containing title, name of the production team along with the logo of MHRD

Hello Friends and Welcome to the spoken tutorial on "loading data from files".
Show Slide

Learning objectives

Read data from files, which contain data in:

  • Single column format
  • Multiple columns separated by spaces or other delimiters
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
Show Slide

System Requirements

  • Ubuntu Linux 14.04
To record this tutorial, I am using
  • Ubuntu Linux 14.04 operating system
  • Python 3.4.3
  • IPython 5.1.0
Show Slide

Pre-requisites

You should know how to run basic Python commands on the ipython console.

If not, for relevant Python tutorials, please visit this website http://spoken-tutorial.org

[Terminal]

ipython3

Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.

Now, type ipython3 and press Enter.

[IPython console]

%pylab and press Enter.

Let us initialise the pylab package.

Type percent pylab and press Enter.

[IPython Terminal]

Type

cat primes.txt

and press Enter

Let us begin with reading the file primes.txt. This file contains a list of prime numbers listed in a column.

Type cat(space)primes(dot)txt

We can use the cat command to fetch data from the file and display it on the terminal.

press Enter

We see the prime numbers are displayed on the terminal.

[IPython Terminal]

Type primes = loadtxt(“primes.txt”)

and press Enter

Now we can use the loadtxt() command to store this list into the variable primes.


Type primes(equal to)loadtxt(within parentheses)(within double quotes)primes(dot)txt and press Enter.

Please make sure that you provide the correct path to the file, 'primes.txt'.

The file, in our case, is present in the home folder.

[IPython Terminal]

Type

print(primes) and press Enter

primes is now a sequence of prime numbers, that was listed in the file, primes.txt.

Now let us display the contents in the variable primes.

So, type, print (within parentheses) primes and press Enter.

We see the sequence printed.

[IPython Terminal]

Highlight the output on the terminal

We observe that all the numbers end with a period ‘.’.

This is because all these numbers are floats.

[IPython Terminal]

Type

cat pendulum.txt

Highlight Cat

and press Enter

Type cat(space)pendulum(dot)txt

Press Enter.

This file contains two columns of data.

This first column contains the length of the pendulum.

The second column contains the corresponding time period.

[IPython Terminal]

Type

pend=loadtxt(“pendulum.txt”)

and press Enter

Let us now read the data from the file into the variable pend using the loadtxt command.

Type pend(equal to)loadtxt(within parentheses)(within double quotes)pendulum(dot)txt and press Enter.

Please note that loadtxt needs both the columns of the file to have equal number of rows.
[IPython Terminal]

print(pend)

and press Enter

Now print the variable pend to see what it contains.

Type print(within parentheses)pend and press Enter.

[IPython Terminal]

Type L,T=loadtxt(“pendulum.txt”, unpack=True)

and press Enter

Notice that variable has two sequences containing two columns of the data file.

Let us use an additional argument of the loadtxt command to read the data into two separate sequences.

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.

[IPython Terminal]

Type print(L)

print(T)

and press Enter

Now print the variables L and T, to see what they contain.

Type print(within parentheses)L and press Enter

Type print(within parentheses)T and press Enter

Highlight the output Notice, that L and T now contain the first and second columns of data from the pendulum.txt respectively

unpack(equal to)True has made the two columns into two separate and simple sequences.

Show Slide 4

Assignment 1

Pause the video over here and try out the following exercise and resume the video.
  • Read the data from the file pendulum(underscore)semicolon(dot)txt.
  • This file contains data in two columns. These columns are separated by semicolons.
  • Use the IPython help to see how to do this.
[IPython Terminal]

Type

L, T = loadtxt(“pendulum_semicolon.txt”, unpack=True,delimiter=”;”)


Let us look at the solution. Switch to the terminal.

First we will see the content of the file.

So type cat space pendulum(underscore)semicolon(dot)txt

Press Enter

We see the two columns separated by a semi-colon.

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.

[IPython Terminal]

Type

print(L)

and press Enter

[IPython Terminal]

Type

print(T)

and press Enter

Then Type print(within parentheses)L and press Enter.


print(within parentheses)T and press Enter.


This will display the contents inside the two variables L and T.

Summary slide This brings us to the end of this tutorial. In this tutorial, we have learnt:

To read data from files using the loadtxt() command.

The data can be in

  • A single column format
  • Or multiple column format, separated by spaces or other delimiters.
Evaluation slide 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.

Evaluation slide
  1. Given a file data.txt with three columns of data separated by colon.

Read it into 3 separate simple sequences.

Slide

Solution 1:

Now let us look at the answers,

The answer to the first question is False.

The loadtxt() command can read data from files having single columns as well as multiple columns.

Slide

Solution 2:

The answer to the second question is,

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

x(equal to)loadtxt(within parentheses and within double quotes)data(dot)txt(after double quotes comma)unpack(equal to)True

Slide


Solution 3:

The answer to the third question is,

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

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)semicolon

Show Slide

Forum

Do you have questions on THIS Spoken Tutorial?

Choose the minute and second where you have the question.

Explain your question briefly.

Someone from the FOSSEE team will answer them.

Please visit this site.

Show Slide

Fossee Forum

Do you have any general / technical questions?

Please visit the forum given in the link.

Show Slide

Textbook Companion

The FOSSEE team coordinates coding of solved examples of popular books.

We give honorarium and certificates for those who do this.

For more details, please visit this site.

Show Slide

Acknowledgment

The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
Show Slide

Thank You

This is Prathamesh Salunke from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Pratham920