如何将函数的数据保存到csv fi

2024-04-19 20:33:22 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我想使用函数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()

Tags: 文件函数testtimemaindeffunctionsome
1条回答
网友
1楼 · 发布于 2024-04-19 20:33:22

这里有一个方法

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)

相关问题 更多 >