0

我正在关注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?

提前感谢您的帮助。

4

1 回答 1

5

好吧,首先,您需要关闭您的父母:

spamwriter.writerow(true_or_false(3))

其次,你需要把它放在一个list--writerow期望一个可迭代的:

spamwriter.writerow([true_or_false(3)])

那应该这样做。

于 2013-10-30T23:43:36.687 回答