<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC2%2Floading-data-from-files%2FEnglish</id>
		<title>Python/C2/loading-data-from-files/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC2%2Floading-data-from-files%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C2/loading-data-from-files/English&amp;action=history"/>
		<updated>2026-04-08T19:59:53Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C2/loading-data-from-files/English&amp;diff=452&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello Friends and Welcome to this tutorial on …'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C2/loading-data-from-files/English&amp;diff=452&amp;oldid=prev"/>
				<updated>2012-11-29T05:57:19Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello Friends and Welcome to this tutorial on …&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello Friends and Welcome to this tutorial on &amp;quot;loading data from files&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of this tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# Read data from files, containing a single column of data&lt;br /&gt;
# Read multiple columns of data, separated by spaces or other delimiters.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 ipython -pylab&lt;br /&gt;
| Let us switch to the terminal and start IPython, using ipython -pylab&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Navigate to the path in the OS, open the file and show it &lt;br /&gt;
| 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. Please make sure that you provide the correct path of the file, 'primes.txt'. The file, in our case, is present in &amp;lt;tt&amp;gt;/home/fossee/primes.txt&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  cat /home/fossee/primes.txt&lt;br /&gt;
| Otherwise we can use the &amp;lt;tt&amp;gt;cat&amp;lt;/tt&amp;gt; command to locate the file and read the contents of it.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  primes = loadtxt('/home/fossee/primes.txt')&lt;br /&gt;
| Now let us read this list into the variable &amp;lt;tt&amp;gt;primes&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  print primes&lt;br /&gt;
| &amp;lt;tt&amp;gt;primes&amp;lt;/tt&amp;gt; is now a sequence of prime numbers, that was listed in the file,``primes.txt``.&lt;br /&gt;
&lt;br /&gt;
We now type, &amp;lt;tt&amp;gt;print primes&amp;lt;/tt&amp;gt; to see the sequence printed.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Highlight the output on the terminal &lt;br /&gt;
| We observe that all the numbers end with a period. This is so, because these numbers are actually read as &amp;lt;tt&amp;gt;floats&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  cat /home/fossee/pendulum.txt&lt;br /&gt;
| Now, let us use the &amp;lt;tt&amp;gt;loadtxt&amp;lt;/tt&amp;gt; command to read a file &amp;lt;tt&amp;gt;pendulum.txt&amp;lt;/tt&amp;gt; that contains two columns of data. This file contains the length of the pendulum in the first column and the corresponding time period in the second. Note that here &amp;lt;tt&amp;gt;loadtxt&amp;lt;/tt&amp;gt; needs both the columns to have equal number of rows.&lt;br /&gt;
&lt;br /&gt;
We use the &amp;lt;tt&amp;gt;cat&amp;lt;/tt&amp;gt; command to view the contents of this file.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  pend = loadtxt('/home/fossee/pendulum.txt')&lt;br /&gt;
| Let us, now, read the data into the variable &amp;lt;tt&amp;gt;pend&amp;lt;/tt&amp;gt;. Again, it is assumed that the file is in &amp;lt;tt&amp;gt;/home/fossee/&amp;lt;/tt&amp;gt;&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  print pend&lt;br /&gt;
| Let us now print the variable &amp;lt;tt&amp;gt;pend&amp;lt;/tt&amp;gt; and see what it contains.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True)&lt;br /&gt;
| Notice that &amp;lt;tt&amp;gt;pend&amp;lt;/tt&amp;gt; is not a simple sequence like &amp;lt;tt&amp;gt;primes&amp;lt;/tt&amp;gt;. It has two sequences, containing both the columns of the data file. Let us use an additional argument of the &amp;lt;tt&amp;gt;loadtxt&amp;lt;/tt&amp;gt; command, to read it into two separate, simple sequences.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  print L&lt;br /&gt;
 print T&lt;br /&gt;
| Let us now, print the variables L and T, to see what they contain.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Notice, that L and T now contain the first and second columns of data from the data file, &amp;lt;tt&amp;gt;pendulum.txt&amp;lt;/tt&amp;gt;, and they are both simple sequences. &amp;lt;tt&amp;gt;unpack=True&amp;lt;/tt&amp;gt; has given us the two columns into two separate sequences instead of one complex sequence.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| Till now, we have learnt the basic use of the &amp;lt;tt&amp;gt;loadtxt&amp;lt;/tt&amp;gt; command. Let us try an example.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
Read the file &amp;lt;tt&amp;gt;pendulum_semicolon.txt&amp;lt;/tt&amp;gt; which contains the same data as &amp;lt;tt&amp;gt;pendulum.txt&amp;lt;/tt&amp;gt;, but the columns are separated by semi-colons instead of spaces. Use the IPython help to see how to do this.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch back to the terminal &lt;br /&gt;
&lt;br /&gt;
 L, T = loadtxt('/home/fossee/pendulum_semicolon.txt', unpack=True,&lt;br /&gt;
                delimiter=';')&lt;br /&gt;
 &lt;br /&gt;
 print L&lt;br /&gt;
 &lt;br /&gt;
 print T&lt;br /&gt;
| &lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4 &lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of this tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# To Read data from files, containing a single column of data using the &amp;lt;tt&amp;gt;loadtxt&amp;lt;/tt&amp;gt; command.&lt;br /&gt;
# To Read multiple columns of data, separated by spaces or other delimiters.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide&lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# &amp;lt;tt&amp;gt;loadtxt&amp;lt;/tt&amp;gt; can read data from a file with one column only. True or False?&lt;br /&gt;
# Given a file &amp;lt;tt&amp;gt;data.txt&amp;lt;/tt&amp;gt; with three columns of data separated by spaces, read it into 3 separate simple sequences.&lt;br /&gt;
# Given a file &amp;lt;tt&amp;gt;data.txt&amp;lt;/tt&amp;gt; with three columns of data separated by &amp;quot;:&amp;quot;, read it into 3 separate simple sequences.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
# False. &amp;lt;tt&amp;gt;loadtxt&amp;lt;/tt&amp;gt; command can read data from files having both single columns as well as multiple columns.&lt;br /&gt;
# A file with three columns of data separated by spaces to be read into 3 separate sequences, we use the loadtxt command as,&lt;br /&gt;
&lt;br /&gt;
Enumerated list ends without a blank line; unexpected unindent.&lt;br /&gt;
&lt;br /&gt;
 x = loadtxt(&amp;quot;data.txt&amp;quot;, unpack=True)&lt;br /&gt;
&lt;br /&gt;
# 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&lt;br /&gt;
&lt;br /&gt;
Enumerated list ends without a blank line; unexpected unindented.&lt;br /&gt;
&lt;br /&gt;
 x = loadtxt(&amp;quot;data.txt&amp;quot;, unpack=True, delimiter=&amp;quot;:&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7 &lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>