Page 147 - PYTHON-12
P. 147

writerow(fields) is going to write the fields which are the column headings into the file and have
               to be written only once. Using for loop, rows are traversed from the list of rows from the file.
               writerow(i) is writing the data row-wise in the for loop and in the last the file is automatically
               closed.

               Also, while giving csv.writer(), the delimiter taken is comma. We can change the delimiter whenever
               and wherever required by changing the argument passed to delimiter attribute.

               For example, delimiter = "|" (pipe symbol). You can put any character as delimiter and if nothing is
               given, comma is placed by default.
               writerow() method is used to write each row.

               In this program, we have used for loop for writing data row-wise onto the file using writerow()
               method. We can avoid using for loop and can write all the rows/records in one go.

               This can be done by using writerows() method. writerows() writes all the rows in one go, so you
               need not use for loop and iterations.
               Practical Implementation–12

               Program to write data onto "student" csv file using writerows() method (modification of Practical
               Implementation -11).

































           Computer Science with Python–XII  4.18
   142   143   144   145   146   147   148   149   150   151   152