Page 146 - PYTHON-12
P. 146
Contents of “marks.csv” created:
Explanation:
In the above program, the very first line is for importing csv file into your program. Next, whatever
are the column headings for our data are mentioned as a list in variable called fields. All the data
stored inside these fields are placed inside variable rows.
Now give the name of your file, let us say, student.csv and that will be created and stored inside
your current working directory or the path that you mentioned (as we have given for D:/) for the
attribute "filename".
‘w’ stands for write mode and we are using the file by opening it using "with open", since using
with open does not require file to be closed explicitly. The next statement comprises the most
Python Libraries
important function used for writing onto csv file, viz., csv.writer(), to obtain a writer object and
store it in the variable csv_w as the name of the variable and this is the CSV object. writer() takes
the name of file object ‘f’ as the argument. By default, the delimiter is comma (,).
4.17