Python3 CSV writerows, TypeError: 'str' does not support the buffer interface
Asked Answered
P

1

9

I am translating the following Kaggle code into Python3.4:

In the final lines when outputting a CSV file,

predictions_file = open("myfirstforest.csv", "wb")
open_file_object = csv.writer(predictions_file)
open_file_object.writerow(["PassengerId","Survived"])
open_file_object.writerows(zip(ids, output))
predictions_file.close()
print('Done.')

there's a Type Error

TypeError: 'str' does not support the buffer interface

which occurs at the line open_file_object.writerow(["PassengerId","Survived"]).

I believe this is because Opening a file in binary mode to write csv data to doesn't work in Python 3. However, adding encoding='utf8' in the open() line doesn't work either.

What is the standard way to do this in Python3.4?

Pneumonectomy answered 30/1, 2016 at 9:51 Comment(0)
M
13

Creating a CSV file is different between Python 2 and Python 3 (as a look into the docs for the csv module would have shown):

Instead of

predictions_file = open("myfirstforest.csv", "wb")

you need to use

predictions_file = open("myfirstforest.csv", "w", newline="")

(And you should use a context manager to handle the closing of the file for you, in case an error does occur):

with open("myfirstforest.csv", "w", newline="") as predictions_file:
    # do stuff
# No need to close the file
Markham answered 30/1, 2016 at 9:54 Comment(2)
"as a look into the docs for the csv module would have shown" I realized this (and mentioned it in the original question, in English). I didn't understand the error was caused by newline=""', which is why I wanted a clarification. I apologize for causing you such distress.Pneumonectomy
@Pneumonectomy for clarity: 'wb' opens the file in a binary mode and therefore .write() expects binary data such as bytes object, not str that is Unicode in Python 3. 'w' opens the file in a text mode: .write() expects str. Python 3.5 produces more intelligible error message: TypeError: a bytes-like object is required, not 'str'. newline="" is unrelated to the issue but it is recommended for csv files (csv module uses b'\r\n' even on Unix; newline='' allows to write newlines without the usual universal newlines mode conversion ('\n' -> os.linesep)).Emden

© 2022 - 2024 — McMap. All rights reserved.