created function to write to csv