Page 145 - PYTHON-12
P. 145
Till now we have covered the basics of how to use the CSV module to read the contents of a CSV file.
Now, we will discuss about how to write to a CSV file in python.
4.15.2 Writing to CSV File
To write to a CSV file in Python, we can use the csv.writer() function. The csv.writer() function
returns a writer object that converts the user's data into a delimited string. This string can later be
used to write into CSV files using the writerow() function.
In order to write to a CSV file, we create a special type of object to write to the CSV file "writer
object", which is defined in the CSV module, and which we create using the writer() function.
The writerow() method allows us to write a list of fields to the file. The fields can be strings
or numbers or both. Also, while using writerow(), you do not need to add a new line character
(or other EOL indicator) to indicate the end of the line, writerow() does it for you as necessary.
Let us write data onto a CSV file using writerow() method.
Practical Implementation–11
Program to write data onto “student” CSV file using writerow() method.
Computer Science with Python–XII 4.16