1

我正在处理的函数应该在每次调用时添加一行新的字符串和数字。
我将一个字符串和一个数字列表作为函数的参数传递。
目前,它仅在调用函数时将参数写入如下:

[1.0, 2.0, 3.0]

但是,我希望函数编写如下分隔的代码:

1.0     2.0     3.0

看起来,我不太了解 writerow 函数。所以我的问题是,如何分隔传递给 writerow 的数字列表?

    # writes the results to a csv file
    # each row contains a string and three numbers
    def write_to_file(file_name, n_t_argument):
        with open(file_name + '.txt', 'a', newline='') as outputfile:
            wrtr  = csv.writer(outputfile, dialect = 'excel-tab')
            text_input = [ n_t_argument ]
            wrtr.writerow(text_input)

    write_to_file('output', [1.0, 2.0, 3.0])
4

1 回答 1

3

你把你的数字放在一个嵌套列表中,一个列表中的列表。无需这样做:

def write_to_file(file_name, n_t_argument):
    with open(file_name + '.txt', 'a', newline='') as outputfile:
        wrtr  = csv.writer(outputfile, dialect = 'excel-tab')
        wrtr.writerow(n_t_argument)
于 2013-04-10T21:49:31.907 回答