For example: Writing to csv file in Python
with open('StockPrice.csv', 'wb') as f:
Why would we need to open in binary for a csv file?
Is this just habit, or is there a use-case for when binary is necessary for a csv file?
It's necessary to use the mode "wb"
when writing output using the csv
module on Windows, because the csv
module will write out line-ends as \r\n
regardless of what platform you're running on.
If you're running on Windows, and you have a file open with mode "w"
, Python will add an extra carriage return every time you write a newline. So if you use a file with mode "w"
to write output using the csv
module, you will end up with \r\r\n
line-endings, as both Python and the csv
module have added carriage-return characters.
Here's a quick program that demonstrates the result. Note that we read the file in binary mode ("rb"
) to prevent Python from replacing \r\n
with \n
as it reads the file back in:
import csv
with open("output.csv", "w") as f:
w = csv.writer(f)
w.writerow([1,2,3,4])
w.writerow([5,6,7,8])
with open("output.csv", "rb") as f:
print repr(f.read())
When I run this on Windows, I get the following output:
'1,2,3,4\r\r\n5,6,7,8\r\r\n'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With