[NumPy-Tickets] [NumPy] #1731: allow loadtxt() to read given number of rows

NumPy Trac numpy-tickets@scipy....
Sat Mar 26 09:43:37 CDT 2011


#1731: allow loadtxt() to read given number of rows
-------------------------+--------------------------------------------------
 Reporter:  rc           |       Owner:  somebody    
     Type:  enhancement  |      Status:  needs_review
 Priority:  normal       |   Milestone:  2.0.0       
Component:  Other        |     Version:  1.5.0       
 Keywords:               |  
-------------------------+--------------------------------------------------
Changes (by dynetrekk):

 * cc: paul.anton.letnes@… (added)
  * status:  new => needs_review


Comment:

 A suggested implementation of the numrows keyword can be found here. It
 simply slices the number of lines in the file. One can of course discuss
 whether one should count blank or commented lines - I believe not, as one
 would want to be able to relate the line number of a file with the number
 of rows to read. (If this is desired, one could write a break statement
 for the for loop.)

 {{{
 diff -r 2763b87dd7e8 numpy/lib/npyio.py
 --- a/numpy/lib/npyio.py        Fri Mar 25 22:37:19 2011 -0600
 +++ b/numpy/lib/npyio.py        Sat Mar 26 15:40:11 2011 +0100
 @@ -579,7 +579,8 @@


  def loadtxt(fname, dtype=float, comments='#', delimiter=None,
 -            converters=None, skiprows=0, usecols=None, unpack=False):
 +            converters=None, skiprows=0, numrows=None, usecols=None,
 +            unpack=False):
      """
      Load data from a text file.

 @@ -609,6 +610,9 @@
          ``converters = {3: lambda s: float(s or 0)}``.  Default: None.
      skiprows : int, optional
          Skip the first `skiprows` lines; default: 0.
 +    numrows : int, optional
 +        Read only `numrows` lines; The default, None, results in all rows
 +        being read.
      usecols : sequence, optional
          Which columns to read, with 0 being the first.  For example,
          ``usecols = (1,4,5)`` will extract the 2nd, 5th and 6th columns.
 @@ -771,7 +775,10 @@
              converters[i] = conv

          # Parse each line, including the first
 -        for i, line in enumerate(itertools.chain([first_line], fh)):
 +        all_lines = itertools.chain([first_line], fh)
 +        if not (numrows is None):
 +            all_lines = itertools.islice(all_lines, numrows)
 +        for i, line in enumerate(all_lines):
              vals = split_line(line)
              if len(vals) == 0:
                  continue
 }}}

-- 
Ticket URL: <http://projects.scipy.org/numpy/ticket/1731#comment:1>
NumPy <http://projects.scipy.org/numpy>
My example project


More information about the NumPy-Tickets mailing list