Python newbie here. I was trying to troubleshoot an issue with writing a csv file in a larger program and decided to go back to basics to try to find the problem.
I
This is a bit late to the party, but a solution I have yet to see outside of a single comment is using with
and as
. In this case, it may look like:
import csv
with csv.writer(open('eggs.csv', 'w'), delimiter=' ', quotechar='|') as spamWriter:
spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
I've used this in the past with no problems.