我正在关注CSV 文档,但我正在切线。如何将函数写入 csv 文件?我正在尝试使用以下代码将 true_or_false(3) 的结果写入单元格,但似乎我缺少括号、撇号或括号。当我尝试编写它时......我收到“无效的语法错误”
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?
提前感谢您的帮助。