Python3: writing csv files
Documentation says that you should use open('eggs.csv', 'w', newline='')
This will work on both Python 2 and Python 3:
if sys.version_info >= (3,0,0): f = open(filename, 'w', newline='')else: f = open(filename, 'wb')
As documented in a footnote:
csv.writer(csvfile, dialect='excel', **fmtparams)
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.
The following variant works on Linux and Windows:
spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL, newline='')spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])