You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Extraneous newlines with csv.writer on Windows #51447
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=<Date2011-03-20.14:31:23.590>created_at=<Date2009-10-24.22:07:03.893>labels= ['type-bug', 'docs']
title='Extraneous newlines with csv.writer on Windows'updated_at=<Date2011-03-20.15:40:06.253>user='https://bugs.python.org/zacktu'
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'.
Your output file should be opened in binary mode. Sounds like you
opened it in text mode.
zacktu commented on Oct 24, 2009
zacktumannequin
MannequinAuthor
More actions
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.
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.
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.
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.
changed the title [-]csv.writer[/-][+]Extraneous newlines with csv.writer on Windows[/+]on Jul 28, 2010
zacktu commented on Jul 28, 2010
zacktumannequin
MannequinAuthor
More actions
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.
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
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).
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:
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.
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.
Activity
zacktu commentedon Oct 24, 2009
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'.
smontanaro commentedon Oct 24, 2009
Your output file should be opened in binary mode. Sounds like you
opened it in text mode.
zacktu commentedon Oct 24, 2009
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.
baloan commentedon Jul 27, 2010
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.
smontanaro commentedon Jul 28, 2010
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.
merwok commentedon Jul 28, 2010
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.
[-]csv.writer[/-][+]Extraneous newlines with csv.writer on Windows[/+]zacktu commentedon Jul 28, 2010
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:
merwok commentedon Jul 28, 2010
If the documentation is not clear enough about requiring binary, it is a doc bug.
(P.S. Please strip unneeded quotes. Thanks)
smontanaro commentedon Jul 28, 2010
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:
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).
12 remaining items
smontanaro commentedon Mar 19, 2011
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:
S
sjmachin commentedon Mar 19, 2011
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.
python-dev commentedon Mar 20, 2011
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
bitdancer commentedon Mar 20, 2011
Fixed now. Thanks, and sorry for the delay, and the confusion.
bitdancer commentedon Mar 20, 2011
Gah, I messed up the push. Now I have to backport the doc fix :(
python-dev commentedon Mar 20, 2011
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
bitdancer commentedon Mar 20, 2011
OK, now it's really done (I hope!).
smontanaro commentedon Mar 20, 2011
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
python-dev commentedon Mar 20, 2011
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