【问题标题】:Python csvwriter is not writing to csv filePython csv writer 未写入 csv 文件
【发布时间】:2019-02-06 11:09:56
【问题描述】:

我是 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 文件?

【问题讨论】:

    标签: python csv tensorflow


    【解决方案1】:

    csvwriter.writerow()之后使用stream.flush()

    Same question

    【讨论】:

    • 你确定你的模型trainpredict已经完成了吗?有时可能需要一段时间
    • A. Albershteyn 我会确认的。很好的洞察力。
    【解决方案2】:

    好的。正如我所说,我是 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!!!**
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 2015-04-24
      • 2015-09-16
      • 2019-11-15
      • 1970-01-01
      相关资源
      最近更新 更多