0

Suppose I have a loop which executes like the following:-

    for i in range(len(X_test)):
      _, _, ref, hyp = result([X_test[i]],y_test.iloc[i])
      hyp = hyp.replace('<end>', '')
      bleu_score = bleu([ref], hyp, smoothing_function=cc.method3)
      print("Associated BLEU score:",bleu_score,"\n")
      overall_bleu.append(bleu_score)

What I would like to do is create a new CSV file, with 2 columns i.e. Reference (Stands for ref variable in the loop) and Predicted (Stands for hyp variable in the loop) such that for each row, at every successive iteration of the loop, the strings hyp and ref are written to corresponding columns of a new CSV file (which I would like to create). Any clean way of doing this? I was thinking to use Pandas, but seems it will be redundant to write every time within the loop.

tawab_shakeel
  • 3,701
  • 10
  • 26
JChat
  • 784
  • 2
  • 13
  • 33
  • you can use module `csv` to create csv file. You can open file before `for` loop and write/append single row to file inside `for` loop. If you have simple strings (without comma or enter) then you can even open normal text file and write/append row as text using comma and enter - this way you can also create csv file. – furas Jun 27 '19 at 08:59

1 Answers1

-1

did you try using csv.DictWriter https://docs.python.org/3/library/csv.html

additionally refer this