Skip to content

Extraneous newlines with csv.writer on Windows #51447

Closed

Description

@zacktu
mannequin
BPO 7198
Nosy @smontanaro, @merwok, @bitdancer

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/bitdancer'
closed_at = <Date 2011-03-20.14:31:23.590>
created_at = <Date 2009-10-24.22:07:03.893>
labels = ['type-bug', 'docs']
title = 'Extraneous newlines with csv.writer on Windows'
updated_at = <Date 2011-03-20.15:40:06.253>
user = 'https://bugs.python.org/zacktu'

bugs.python.org fields:

activity = <Date 2011-03-20.15:40:06.253>
actor = 'python-dev'
assignee = 'r.david.murray'
closed = True
closed_date = <Date 2011-03-20.14:31:23.590>
closer = 'r.david.murray'
components = ['Documentation']
creation = <Date 2009-10-24.22:07:03.893>
creator = 'zacktu'
dependencies = []
files = []
hgrepos = []
issue_num = 7198
keywords = []
message_count = 26.0
messages = ['94437', '94438', '94441', '111694', '111773', '111787', '111792', '111793', '111832', '111910', '124580', '124598', '124678', '124682', '126593', '131400', '131417', '131418', '131443', '131468', '131469', '131475', '131495', '131496', '131498', '131501']
nosy_count = 7.0
nosy_names = ['skip.montanaro', 'sjmachin', 'baloan', 'eric.araujo', 'r.david.murray', 'zacktu', 'python-dev']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue7198'
versions = ['Python 3.1', 'Python 2.7', 'Python 3.2']

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 24, 2009
  2. zacktu commented on Oct 24, 2009

    zacktumannequin
    MannequinAuthor

    I used csv.writer to open a file for writing with comma as separator and
    dialect='excel'. I used writerow to write each row into the file. When
    I execute under linux, each line is terminated by '\r\n'. When I
    execute under windows, each line is terminated by '\r\r\n'. Thus, under
    MS Windows, when I read the csv file, there is a blank line between each
    significant line. I have dropped cvs.writer and now build each line
    manually and terminate it with '\n'. When the line is written in
    windows, it is terminated by '\r\n'. That's what should happen.

    As I see it, writerow with dialect='excel' should only terminate a line
    with '\n'. Windows will automatically place a '\r' in front of the '\n'.

  3. smontanaro commented on Oct 24, 2009

    @smontanaro
    Contributor

    Your output file should be opened in binary mode. Sounds like you
    opened it in text mode.

  4. zacktu commented on Oct 24, 2009

    zacktumannequin
    MannequinAuthor

    Probably so. I'm sorry to report this as a bug if it's not. I asked
    abut this on a Python group on IRC and got no suggestions. Thanks for
    taking a look.

  5. baloan commented on Jul 27, 2010

    baloanmannequin
    Mannequin

    I encountered the same problem. It is unclear that using binary mode for the file is solving the problem. I suggest to add a hint to the documentation.

  6. smontanaro commented on Jul 28, 2010

    @smontanaro
    Contributor

    Can you provide me with a concrete example which fails for you?
    I don't have ready access to a Windows machine with Python on
    it but should be able to arrange something at work, however before
    going through the exercise of spending admin time to install
    Python I would like to look at code which fails for you first.

  7. self-assigned this
    on Jul 28, 2010
  8. merwok commented on Jul 28, 2010

    @merwok
    Member

    Bob, can you give us some code to reproduce the problem, in the form or a unit test or even just a regular function? It will help confirm the bug and fix it.

  9. changed the title [-]csv.writer[/-] [+]Extraneous newlines with csv.writer on Windows[/+] on Jul 28, 2010
  10. zacktu commented on Jul 28, 2010

    zacktumannequin
    MannequinAuthor

    Eric,

    This issue was resolved for me by Skip Montanaro's response less than an
    hour after I posted it. I didn't understand why a text file had to be
    binary, but I no longer had a problem with extraneous. In looking back
    at my message 94441, I think that it was ambiguous and that I should
    have made it clear that I no longer had a problem. Perhaps in my
    ignorance as a newbie I didn't close the issue properly.

    I don't know that I can reproduce the problem any more. I think that I
    was writing snippets of code to try to isolate the problem and when I
    used Skip's solution I changed the program and deleted the test code.

    Please let me know what I can do to help you now.

    Bob

    Éric Araujo wrote:

    Éric Araujo <merwok@netwok.org> added the comment:

    Bob, can you give us some code to reproduce the problem, in the form or a unit test or even just a regular function? It will help confirm the bug and fix it.

    ----------
    nosy: +merwok
    stage: -> unit test needed
    title: csv.writer -> Extraneous newlines with csv.writer on Windows


    Python tracker <report@bugs.python.org>
    <http://bugs.python.org/issue7198\>


  11. merwok commented on Jul 28, 2010

    @merwok
    Member

    If the documentation is not clear enough about requiring binary, it is a doc bug.

    (P.S. Please strip unneeded quotes. Thanks)

  12. smontanaro commented on Jul 28, 2010

    @smontanaro
    Contributor

    I got access to Python 2.6.5 on Windows and ran this simple
    example:

    Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
    Type "copyright", "credits" or "license()" for more information.

        ****************************************************************
        Personal firewall software may warn about the connection IDLE
        makes to its subprocess using this computer's internal loopback
        interface.  This connection is not visible on any external
        interface and no data is sent to or received from the Internet.
        ****************************************************************
        
    IDLE 2.6.5      
    >>> f = open("H:sample.csv", "wb")
    >>> import csv
    >>> writer = csv.writer(f)
    >>> writer.writerow([1,2,3])
    >>> writer.writerow(['a', 'b', 'c'])
    >>> del writer
    >>> f.close()
    >>> 

    I then looked at the CSV file which it generated.
    Looked find to me. Each of the two rows was terminated
    by a single CRLF pair.

    Then I repeated the "test", opening the file in text
    mode:

    >> f = open("H:sample2.csv", "w")

    >> writer = csv.writer(f)

    >> writer.writerow([1,2,3])

    >> writer.writerow(['a', 'b', 'c'])

    >> del writer

    >> f.close()

    >>

    That output does indeed terminate each line with
    CRCRLF and when viewed in a spreadsheet program
    such as OpenOffice Calc (probably Excel as well),
    displays a blank line between the 123 row and the
    abc row.

    I've removed the "unit test needed" attribute from the
    ticket as there is a test_writerows test case in the
    Python test suite. Also closing again and marking
    invalid. If you still believe there is actually a
    problem, feel free to reopen this issue, but also
    please send me (skip@pobox.com) a short example and
    the erroneous output it produces for you (attach your
    two files - don't just embed them in your mail msg).

  13. 12 remaining items

  14. smontanaro commented on Mar 19, 2011

    @smontanaro
    Contributor

    Actually, I was thinking of another doc patch for the csv module.
    Your changes (or something very like them) made it into the 3.2
    release, as you can see here:

    http://docs.python.org/py3k/library/csv.html
    

    S

  15. sjmachin commented on Mar 19, 2011

    sjmachinmannequin
    Mannequin

    Skip, The changes that I suggested have NOT been made. Please re-read the doc page you pointed to. The "writer" paragraph does NOT mention that newline='' is required when writing. The "writer" examples do NOT include newline=''. The examples have NOT been enhanced by using a "with" statement and not using space as an example delimiter.

    PLEASE RE-OPEN THIS ISSUE.

  16. python-dev commented on Mar 20, 2011

    python-devmannequin
    Mannequin

    New changeset ab27f16f707a by R David Murray in branch 'default':
    bpo-7198: add newlines='' to csv.writer docs.
    http://hg.python.org/cpython/rev/ab27f16f707a

    New changeset 959f666470cc by R David Murray in branch 'default':
    Merge bpo-7198 doc fix.
    http://hg.python.org/cpython/rev/959f666470cc

    New changeset 9d1b1a95bc8f by R David Murray in branch 'default':
    Merge bpo-7198 doc fix.
    http://hg.python.org/cpython/rev/9d1b1a95bc8f

  17. bitdancer commented on Mar 20, 2011

    @bitdancer
    Member

    Fixed now. Thanks, and sorry for the delay, and the confusion.

  18. bitdancer commented on Mar 20, 2011

    @bitdancer
    Member

    Gah, I messed up the push. Now I have to backport the doc fix :(

  19. python-dev commented on Mar 20, 2011

    python-devmannequin
    Mannequin

    New changeset 9201455f950b by R David Murray in branch '3.1':
    bpo-7198: really add newline='' to csv.writer docs.
    http://hg.python.org/cpython/rev/9201455f950b

    New changeset fa0563f3b7f7 by R David Murray in branch '3.2':
    Really merge bpo-7198
    http://hg.python.org/cpython/rev/fa0563f3b7f7

    New changeset ed0d1e07ce79 by R David Murray in branch 'default':
    Dummy merge bpo-7198
    http://hg.python.org/cpython/rev/ed0d1e07ce79

  20. bitdancer commented on Mar 20, 2011

    @bitdancer
    Member

    OK, now it's really done (I hope!).

  21. smontanaro commented on Mar 20, 2011

    @smontanaro
    Contributor

    John> Skip, The changes that I suggested have NOT been made. Please
    John> re-read the doc page you pointed to. The "writer" paragraph does
    John> NOT mention that newline='' is required when writing. The "writer"
    John> examples do NOT include newline=''. The examples have NOT been
    John> enhanced by using a "with" statement and not using space as an
    John> example delimiter.

    I copied the statement about using newline= from the reader() doc to the
    writer() doc. All the examples I see (I'm looking at the cpython repo -
    that is, what will be 3.3) use the with statement and open files using
    newline=''. I don't think more changes are necessary. I will consult with
    other Python developers about merging these changes to other active
    branches. I simply don't understand the new Mercurial workflow well enough
    to do it properly.

    Skip

  22. python-dev commented on Mar 20, 2011

    python-devmannequin
    Mannequin

    New changeset 88876a264ebe by R David Murray in branch '3.1':
    Markup fixes for bpo-7198 patch.
    http://hg.python.org/cpython/rev/88876a264ebe

    New changeset d0d1235cb66e by R David Murray in branch '3.2':
    Merge markup fixes for bpo-7198 patch.
    http://hg.python.org/cpython/rev/d0d1235cb66e

    New changeset 2a8580f4897c by R David Murray in branch 'default':
    Markup fixes for bpo-7198 patch.
    http://hg.python.org/cpython/rev/2a8580f4897c

  23. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docsDocumentation in the Doc dirtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions