Reputation: 251
I'm following the CSV documentation and I am off on a tangent. How do I write a function into a csv file? I'm trying to write true_or_false(3)'s results to a cell with the following code but it seems like I am missing a bracket, an apostrophe or a parentheses. When I try to write it...I get an "invalid syntax error"
import csv
#import module
x = 5
def true_or_false(n):
return n > x
print true_or_false(3)
with open('eggs.csv', 'wb') as csvfile:
#open csv file
spamwriter = csv.writer(csvfile, delimiter= ' ',
quotechar= '|', quoting =csv.QUOTE_MINIMAL)
#quotechar is the character between apostrophes
spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
# wrote down this
spamwriter.writerow(['Spam' 'Lovely Spam', 'Wonderful Spam'])
# wrote down that
spamwriter.writerow(true_or_false(3)
# how to write?
Thanks for your help in advance.
Upvotes: 0
Views: 204
Reputation: 8492
Well, first, you need to close your paren:
spamwriter.writerow(true_or_false(3))
And second, you need to put it inside a list
-- writerow
expects an iterable:
spamwriter.writerow([true_or_false(3)])
That should do it.
Upvotes: 5