When opening files to be used with module csv
on python 3 you always should add newline=""
the the open statement:
import sys
mode = 'w'
if sys.version_info[0] < 3:
mode = 'wb'
# python 3 write
with open("somefile.txt", mode, newline="") as f:
pass # do something with f
The newline
parameter does not exist in python 2 - but if you skip it in python 3 you get misshaped csv output on windows with additional empty lines in it.
See csv.writer (python 3):
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\n
linendings on write an extra \r
will be added. It should always be safe to specify newline=''
, since the csv module does its own (universal) newline handling.
You should use a contextmanaging with
as well:
with open("somefile.txt", mode) as f: # works in 2 and 3
pass # do something with f
to get your filehandles closed even if you run into some kind of exception. This is python 2 safe - see Methods of file objects:
It is good practice to use the with
keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way. It is also much shorter than writing equivalent try-finally
blocks.
Your solution - ugly but works:
import sys
python3 = sys.version_info[0] >= 3
if python3:
with open("somefile.txt","w",newline="") as f:
pass
else:
with open("somefile.txt","wb") as f:
pass
The problem is the parameter newline
does not exist in python 2. To fix that you would have to wrap/monkypath open(..)
including the contextmanaging.
with
statements, I didn't put in the working example only for brevity. I think however you answer doesn't answer to my question and it's wrong because the code you wrote won't work inpython2.7. – Rickrickard