Hello friends and welcome to the spoken tutorial on "loading data from files".
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.
To record this tutorial, I am using: Ubuntu Linux 14.04 operating system, Python 3.4.3, IPython 5.1.0.
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
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously. Now, type ipython3 and press Enter.
Let us initialise the pylab package. Type percent pylab 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.
Now we can use the loadtxt() command to store this list into the variable primes.
So, 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.
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.
We observe that all the numbers end with a period ‘.’ . This is because all these numbers are floats.
Now, type: cat(space)pendulum(dot)txt and 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.
Let us now read the data from the file into the variable pend using the loadtxt command.
So, 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.
Now, print the variable pend to see what it contains. Type: print(within parentheses)pend and press Enter.
Notice that the 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.
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.
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.
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.
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.
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 and press Enter. We see the two columns separated by a 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.
Now, 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.
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.
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.
3. Given a file data.txt with three columns of data separated by colon. Read it into 3 separate simple sequences.
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.
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.
The answer to the third question is- We read into three separate sequences by using an additional argument of delimiter in the loadtxt command.
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.
Do you have questions on THIS Spoken Tutorial?. Please visit this site.
Do you have any general / technical questions? Please visit the forum given in the link.
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.
The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India.
This is Prathamesh Salunke from IIT Bombay, signing off. Thanks for watching.