-1

I'm a python newbie and I am having a problem with csv module.

My code creates the csv file sucessfully but the csv file remains empty.

Here is my code:

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()    

My question is how do I get python to flush to disk and write the csv file?

Sare
  • 1
  • 2

2 Answers2

0

Use stream.flush() after csvwriter.writerow()

Same question

alberand
  • 632
  • 5
  • 13
0

OK. As I said I'm a newbie to python and I now get the value of indentation.

The following code words perfectly.

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!!!**
Sare
  • 1
  • 2