loading-data-from-files.rst
changeset 192 3a839dca2bdb
parent 144 476ea1730aee
child 199 680a0692529f
equal deleted inserted replaced
191:08b2cb94c57c 192:3a839dca2bdb
    10 been using analytic functions to generate a sequence of points and
    10 been using analytic functions to generate a sequence of points and
    11 plotting them, against another sequence of points. But, this is not
    11 plotting them, against another sequence of points. But, this is not
    12 what we do most often. We often require to plot points obtained from
    12 what we do most often. We often require to plot points obtained from
    13 experimental observations.
    13 experimental observations.
    14 
    14 
    15 #[punch: the initial part of the paragraph may be removed, to make
    15 .. #[punch: the initial part of the paragraph may be removed, to make
    16 this a more generic LO?]
    16     this a more generic LO?]
       
    17 
       
    18 .. #[Nishanth]: The paragraph can be removed.
    17 
    19 
    18 In this tutorial we shall learn to read data from files and save it
    20 In this tutorial we shall learn to read data from files and save it
    19 into sequences that can later be used to plot.
    21 into sequences that can later be used to plot.
    20 
    22 
    21 {{{ Show the outline for this tutorial }}} 
    23 {{{ Show the outline for this tutorial }}} 
    22 
    24 
    23 We shall use the ``loadtxt`` command to load data from files. We will
    25 We shall use the ``loadtxt`` command to load data from files. We will
    24 be looking at how to get multiple columns of data into multiple
    26 be looking at how to get multiple columns of data into multiple
    25 sequences.
    27 sequences.
       
    28 
       
    29 .. #[Nishanth]: can be "How to read a file with multiple columns of
       
    30                 data and load each column of data into a sequence."
    26 
    31 
    27 {{{ switch back to the terminal }}}
    32 {{{ switch back to the terminal }}}
    28 
    33 
    29 As usual, let us start IPython, using 
    34 As usual, let us start IPython, using 
    30 ::
    35 ::
    33 
    38 
    34 Now, Let us begin with reading the file primes.txt, which contains
    39 Now, Let us begin with reading the file primes.txt, which contains
    35 just a list of primes listed in a column, using the loadtxt command.
    40 just a list of primes listed in a column, using the loadtxt command.
    36 The file, in our case, is present in ``/home/fossee/primes.txt``.
    41 The file, in our case, is present in ``/home/fossee/primes.txt``.
    37 
    42 
    38 #[punch: do we need a slide for showing the path?]
    43 .. #[punch: do we need a slide for showing the path?]
    39 
    44 
    40 We use the ``cat`` command to see the contents of this file. 
    45 We use the ``cat`` command to see the contents of this file. 
    41 
    46 
    42 #[punch: should we show the cat command here? seems like a good place
    47 #[punch: should we show the cat command here? seems like a good place
    43 to do it] ::
    48 to do it] ::
    44 
    49 
    45   cat /home/fossee/primes.txt
    50   cat /home/fossee/primes.txt
       
    51 
       
    52 .. #[Nishanth]: A problem for windows users.
       
    53                 Should we simply open the file and show them the data
       
    54                 so that we can be fine with GNU/Linux ;) and windows?
    46 
    55 
    47 Now let us read this list into the variable ``primes``.
    56 Now let us read this list into the variable ``primes``.
    48 ::
    57 ::
    49 
    58 
    50   primes = loadtxt('/home/fossee/primes.txt')
    59   primes = loadtxt('/home/fossee/primes.txt')
    69 This is how we look at the contents of the file, ``pendulum.txt``
    78 This is how we look at the contents of the file, ``pendulum.txt``
    70 ::
    79 ::
    71 
    80 
    72   cat /home/fossee/pendulum.txt
    81   cat /home/fossee/pendulum.txt
    73 
    82 
       
    83 .. #[Nishanth]: The first column is L values and second is T values
       
    84                 from a simle pelculum experiment.
       
    85                 Since you are using the variable names later in the
       
    86                 script.
       
    87                 Not necessary but can be included also.
       
    88 
    74 Let us, now, read the data into the variable ``pend``. Again, it is
    89 Let us, now, read the data into the variable ``pend``. Again, it is
    75 assumed that the file is in ``/home/fossee/``
    90 assumed that the file is in ``/home/fossee/``
    76 ::
    91 ::
    77 
    92 
    78   pend = loadtxt('/home/fossee/pendulum.txt')
    93   pend = loadtxt('/home/fossee/pendulum.txt')
    88 two separate, simple sequences.
   103 two separate, simple sequences.
    89 ::
   104 ::
    90 
   105 
    91   L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True)
   106   L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True)
    92 
   107 
       
   108 .. #[Nishanth]: It has a sequence of items in which each item contains
       
   109                 two values. first is l and second is t
       
   110 
    93 Let us now, print the variables L and T, to see what they contain.
   111 Let us now, print the variables L and T, to see what they contain.
    94 ::
   112 ::
    95 
   113 
    96   print L
   114   print L
    97   print T
   115   print T
       
   116 
       
   117 .. #[Nishanth]: Stress on ``unpack=True`` ??
    98 
   118 
    99 Notice, that L and T now contain the first and second columns of data
   119 Notice, that L and T now contain the first and second columns of data
   100 from the data file, ``pendulum.txt``, and they are both simple
   120 from the data file, ``pendulum.txt``, and they are both simple
   101 sequences.
   121 sequences.
   102 
   122 
   113 finished, resume the video to look at the solution.
   133 finished, resume the video to look at the solution.
   114 
   134 
   115 {{{ switch back to the terminal }}}
   135 {{{ switch back to the terminal }}}
   116 ::
   136 ::
   117 
   137 
   118   L, T = loadtxt('/home/fossee/pendulum.txt', unpack``True, delimiter``';')
   138   L, T = loadtxt('/home/fossee/pendulum.txt', unpack=True, delimiter=';')
   119 
   139 
   120   print L
   140   print L
   121 
   141 
   122   print T
   142   print T
       
   143 
       
   144 .. #[Nishanth]: L, T = loadtxt('/home/fossee/pendulum_semicolon.txt', ...)
   123 
   145 
   124 This brings us to the end of this tutorial. 
   146 This brings us to the end of this tutorial. 
   125 
   147 
   126 {{{ show the summary slide }}}
   148 {{{ show the summary slide }}}
   127 
   149