如何将函数的数据保存到csv文件

时间:2016-07-09 03:45:07

标签: python python-3.x csv

假设我想要使用函数test_function并将其数据写入csv文件,以后可以通过python读取。我怎么能这样做?

只是一个示例函数:

def test_function():
    games_won = 4
    games_lost = 5
    average_time_sec = 6.0
    some_list = [1,2,3,4]
    some_tuple = (1,2,3,4)

编辑: 这就是我试过的:

def test_function():
    games_won = 4
    games_lost = 5
    average_time_sec = 6.0
    some_list = [1,2,3,4]
    some_tuple = (1,2,3,4)

def main():
    output.write(test_function)

main()

1 个答案:

答案 0 :(得分:1)

这是一种方式

import csv
import os.path

resultFile = open("path to your destination file" , 'wb')

wr = csv.writer(resultFile, dialect='excel')

some_list = [1,2,3,4]

wr.writerow(some_list)