Python-3.4.3/C2/Loading-Data-From-Files/English
Loading data from files - English
Title of script: Loading data files from files
Author: Ankit
Keywords: video tutorial, read data, files, loadtxt, ipython
|
|
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:
|
In this tutorial you will learn to,
Read data from files, which contain data in:
Or
|
Show Slide
System Requirements
|
To record this tutorial, I am using
|
Show Slide
Pre-requisites |
You should know how to run basic Python commands on the ipython console.
|
[Terminal]
ipython3
|
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.
|
[IPython console]
%pylab and press Enter. |
Let us initialise the pylab package.
|
[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.
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.
|
Please make sure that you provide the correct path to the file, 'primes.txt'.
| |
[IPython Terminal]
Type print(primes) and press Enter |
primes is now a sequence of prime numbers, that was listed in the file, primes.txt.
|
[IPython Terminal]
Highlight the output on the terminal |
We observe that all the numbers end with a period ‘.’.
|
[IPython Terminal]
Type cat pendulum.txt
|
Type cat(space)pendulum(dot)txt
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.
|
Please note that loadtxt needs both the columns of the file to have equal number of rows.
| |
[IPython Terminal]
and press Enter |
Now print the variable pend and to see what it contains.
|
[IPython Terminal]
|
Notice that variable has two sequences containing two columns of the data file.
|
[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)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
|
Show Slide 4
Assignment 1 |
Pause the video over here and try out the following exercise and resume the video.
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]
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 pendulum(underscore)semicolon(dot)txt Press Enter We see the two columns separated by a semi-colon.
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.
|
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
or
|
Evaluation slide | Here are some self assessment questions for you to solve
Read it into 3 separate simple sequences. |
Evaluation slide | # Given a file data.txt with three columns of data separated by ":"
Read it into 3 separate simple sequences. |
Slide
Solution 1: |
Now let us look at the answers,
|
Slide
Solution 2:
|
To separate data into three columns, we use the loadtxt() command as follows:
|
Slide
|
The answer to the third question is,
|
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. |