Python 2 And 3 Csv Module Text-binary Mode Backwards Compatibility
Solution 1:
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 withopen("somefile.txt", mode, newline="") as f:
pass# do something with fThe 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.
If csvfile is a file object, it should be opened with
newline=''. Ifnewline=''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.
You should use a contextmanaging with as well:
withopen("somefile.txt", mode) as f: # works in 2 and 3pass# do something with fto 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
withkeyword 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 equivalenttry-finallyblocks.
Your solution - ugly but works:
import sys
python3 = sys.version_info[0] >= 3if python3:
withopen("somefile.txt","w",newline="") as f:
passelse:
withopen("somefile.txt","wb") as f:
passThe problem is the parameternewline does not exist in python 2. To fix that you would have to wrap/monkypath open(..) including the contextmanaging.
Post a Comment for "Python 2 And 3 Csv Module Text-binary Mode Backwards Compatibility"