Adding loop functionality to write to a csv file