DEV Community

Tommi k Vincent
Tommi k Vincent

Posted on

Writing data to CSV file in python using Writer object.

A Writer object lets you write data to a CSV file, To create a a writer object, you use the csv.writer() function.Enter the following code below into interactive shell and get to understand it .

import csv

outputFile = open('output.csv', 'w', newline='')
outputWriter = csv.writer(outputFile)
outputWriter.writerow(['kenya', 'Uganda', 'Tanzania', 'Ethiopia'])

outputWriter.writerow(['Javascript','python','php','kotlin','vb'])
outputWriter.writerow([1, 2, 3.141592, 4])


outputFile.close()
Enter fullscreen mode Exit fullscreen mode

call open() and pass it 'w' to open a file in write mode u. This will create the object you can then pass to csv.writer() v to create a Writer object and finally output.close will prevent the file for further execution.

Top comments (0)