Issue 30324: Error using newline='' when writing to CSV file (original) (raw)

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\parser.py", line 91, in     write_header_to_csv()   File "C:\Python\parser\parser.py", 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.
History
Date User Action Args
2022-04-11 14:58:46 admin set github: 74509
2017-05-10 02:16:15 martin.panter set messages: +
2017-05-10 00:43:31 BeamPower set messages: +
2017-05-09 22:41:48 martin.panter set status: open -> closednosy: + martin.pantermessages: + resolution: not a bugstage: resolved
2017-05-09 22:34:24 BeamPower set messages: + versions: + Python 2.7
2017-05-09 22:31:41 BeamPower create