This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Error using newline='' when writing to CSV file
Type: Stage: resolved
Components: Versions: Python 2.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BeamPower, martin.panter
Priority: normal Keywords:

Created on 2017-05-09 22:31 by BeamPower, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg293356 - (view) Author: (BeamPower) Date: 2017-05-09 22:31
I am using the newline='' feature in my program to avoid the extra lines being placed between each line being written to a CSV file.
This error is odd though... I used it before on a previous rev, and it worked fine.
I think the CSV file writing feature has been available since Python 2.3?

Here's the error I am getting:

Traceback (most recent call last):
  File "C:\Python\parser\", line 91, in <module>
  File "C:\Python\parser\", line 44, in write_header_to_csv
    csvfile = open('decodedfile.csv', 'w', newline='')
TypeError: 'newline' is an invalid keyword argument for this function

At the top of my program, I have the following imports:
import sys
import binascii
import csv
msg293357 - (view) Author: (BeamPower) Date: 2017-05-09 22:34
Sorry, I left out a detail... I am running Python 2.7.13
msg293358 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-05-09 22:41
In Python 2, the "open" function doesn't have a "newline" parameter. Perhaps you were looking at the wrong documentation version.
msg293368 - (view) Author: (BeamPower) Date: 2017-05-10 00:43
Sorry, my mistake. I thought I was running Python 2.7, but I was running Python 3.1.

By the way, is there a solution to the extra lines being written to a CSV file using Python 2.7?

Thank you.
msg293370 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2017-05-10 02:16
On Python 2, I'm guessing you are getting extra CRs on Windows? It looks like you have to open in binary mode there to avoid newline translation. This is documented for the "reader" and "writer" functions.
Date User Action Args
2022-04-11 14:58:46adminsetgithub: 74509
2017-05-10 02:16:15martin.pantersetmessages: + msg293370
2017-05-10 00:43:31BeamPowersetmessages: + msg293368
2017-05-09 22:41:48martin.pantersetstatus: open -> closed

nosy: + martin.panter
messages: + msg293358

resolution: not a bug
stage: resolved
2017-05-09 22:34:24BeamPowersetmessages: + msg293357
versions: + Python 2.7
2017-05-09 22:31:41BeamPowercreate