khtad
khtad

Reputation: 1175

Python3 csv writer failing, exiting on error "TypeError: 'newline' is an invalid keyword argument for this function

What I'm trying to do:

I'm trying to change the formatting on a csv file from space delimited to comma delimited.

What I've done:

I can ingest the csv file just fine, and print the output row-by-row to the console. That code looks like this:

with open(txtpath, mode='r', newline='') as f:
    fReader = csv.reader(f)
        for rows in fReader:
            print(rows)

This does exactly what it's supposed to, and spot checking the output confirms that the rows are being read correctly.

The Problem:

According to the official Python3 Documentation on csv.writer, "If csvfile is a file object, it should be opened with newline='' 1." My code looks like this:

with open(csvpath, 'w') as g:
        gWriter = csv.writer(g, newline='')
        gWriter.writerows(rows)

so all together, it looks like this:

with open(txtpath, mode='r', newline='') as f:
    fReader = csv.reader(f)
    for rows in fReader:
        print(rows)
        with open(csvpath, 'w') as g:
            gWriter = csv.writer(g, newline='')
            gWriter.writerows(rows)

However, when I run the code with both Pycharm (Anacondas 3.4 selected as project interpreter) and from the console with python3 mycode.py, both results tell me that newline "is an invalid keyword argument for this function" and references line 42, which is where my writer object is instantiated. I ran it through the debugger and it craps out as soon as I try to create the writer object. If I don't add the newline argument it asks for a dialect specification, so that doesn't work, either.

I'm sure there's something blindingly obvious that I'm missing, but I can't see it.

Upvotes: 5

Views: 33625

Answers (5)

baongocpham.se
baongocpham.se

Reputation: 11

I met the same problem and the solution may sound funny but it does work. I actually changed the position of the newline argument to be in front of mode as below:

with open("database.csv", newline="", mode="a") as database2:

You can test and see the result.

Upvotes: 1

Raju Saladi
Raju Saladi

Reputation: 211

To avoid this error , open the file in 'wb' mode instead of 'w' mode. This will eliminate the need for newline. Corrected code is as follows:

with open(csvpath, 'wb') as g:
    gWriter = csv.writer(g)
    gWriter.writerows(rows)

Upvotes: 10

Ank_247shbm
Ank_247shbm

Reputation: 540

The following will do

with open('filename_inCSV.csv',mode='r') as csvfile:

or

with open('filename_inCSV.csv','r') as csvfile:

Upvotes: 0

Mina Davari
Mina Davari

Reputation: 46

newline does not work in with open('output.csv', 'a',newline='') as fp. It will return back an error:

'newline' is an invalid keyword argument for this function

I used 'ab' method and it worked without blank lines between the lines:

with open('output.csv', 'ab',) as fp

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799180

The wording is just a bit vague. The file should be opened with newline='', but newline is not a valid option for csv.writer() itself.

Upvotes: 9

Related Questions