Python csvwriter没有写入csv-fi

2024-05-17 19:44:49 发布

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

我是一个python新手,csv模块有问题。在

我的代码成功地创建了csv文件,但csv文件仍为空。在

这是我的代码:

with open('log.csv', 'w') as stream:
    csvwriter = csv.writer(stream, delimiter=',', quotechar='"')

    for i in range(0, 200):
        model.train(input_fn=train_input_fn, steps=100)
        evaluation_result = model.evaluate(input_fn=test_input_fn)

        predictions = list(model.predict(input_fn=test_input_fn))
        prediction_result = betting.test_betting_stategy(predictions, test_features, test_labels)

        csvwriter.writerow([(i + 1) * 100, evaluation_result['accuracy'], evaluation_result['average_loss'], prediction_result['performance']])
stream.close()    

我的问题是如何让python刷新到磁盘并写入csv文件?在


Tags: 文件csv代码testinputstreammodeltrain
2条回答

在^{之后使用stream.flush()

Same question

好吧。正如我说的,我是python的新手,现在我得到了缩进的值。在

下面的代码词完美无瑕。在

with open('log.csv', 'w') as stream:
csvwriter = csv.writer(stream, delimiter=',', quotechar='"')

for i in range(0, 200):
    model.train(input_fn=train_input_fn, steps=100)
    evaluation_result = model.evaluate(input_fn=test_input_fn)

    predictions = list(model.predict(input_fn=test_input_fn))
    prediction_result = betting.test_betting_stategy(predictions, test_features, test_labels)

    csvwriter.writerow([(i + 1) * 100, evaluation_result['accuracy'], evaluation_result['average_loss'], prediction_result['performance']])
    stream.flush() **#This indentation works perfectly!!!**

相关问题 更多 >