[Csv] Removal of csv doc about .next()

skip at pobox.com skip at pobox.com
Mon Mar 9 03:37:03 CET 2009


    Jervis> -.. method:: csvreader.next()
    Jervis> -
    Jervis> -   Return the next row of the reader's iterable object as a list,
    Jervis> parsed according
    Jervis> -   to the current dialect.

    Jervis> csvreader no longer has this public method (py3k). Can we remove
    Jervis> this from the docs and replace with a suitable substitute
    Jervis> (information about using next(csvreader) )?

This is the first I've heard about it (well, except for the point being
raised by a comment of yours in issue 1818 on bugs.python.org).  Is it
because Python 3.x has a next() builtin?

Crap...  This completely snuck by me:

    >>> csv.reader(open("f.csv", "rb")).next()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: '_csv.reader' object has no attribute 'next'
    >>> dir(csv.reader(open("f.csv", "rb")))
    ['__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'dialect', 'line_num']
    >>> next(csv.reader(open("f.csv", "rb")))
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    _csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
    >>> next(csv.reader(open("f.csv", "r")))
    ['col1', 'col2', 'color']

This demonstrates a larger bug in the 3.x version of the csv module.  You
can no longer open CSV files in binary mode, yet that is precisely what the
documentation has always said:

    If csvfile is a file object, it must be opened with the $,1rx(Bb$,1ry(B flag on
    platforms where that makes a difference.

Opening a file in binary mode now returns bytes.  Something's got to give.

Skip


More information about the Csv mailing list