I have the below code that was working fine and then started throwing this error. I have a csv file that I am trying to write one row to. While other solutions involve conve
In Python 3, the csv module expects you to give it a file in text mode:
with open('data.csv', 'w', newline='') as out:
The newline='' argument gives the csv module control over how newlines are written (the reason why in Python 2 you opened the file in binary mode).
From the csv.writer() documentation:
If csvfile is a file object, it should be opened with
newline=''.[...]
If
newline=''is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use\r\nlinendings on write an extra\rwill be added. It should always be safe to specifynewline='', since the csv module does its own (universal) newline handling.
Because you gave the module a binary-mode file, you can only write bytes data to it.