On my Windows box, I usually did this in python 2 to write a csv file:
import csv
f = open("out.csv","wb")
cr = csv.writer(f,delimiter=';')
cr.writerow(["a","b","c"])
f.close()
Now that python 3 forbids writing text files as binary, that piece of code does not work anymore. That works:
import csv
f = open("out.csv","w",newline='')
cr = csv.writer(f,delimiter=';')
cr.writerow(["a","b","c"])
f.close()
Problem is: newline
parameter is unknown to Python 2.
Of course, omitting the newline results in a csv file with too many \r
chars, so not acceptable.
I'm currently performing a backwards compatible process to progressively migrate from python 2 to python 3.5 There are a lot of those statements in all my modules.
My solution was embedding the code in a custom module, and the custom module returns file handler + writer object. A python version check is done inside the module, which allows any module using my module to work whatever python version without too much hacking.
Is there a better way?