CSV File Handling Doc
CSV File Handling Doc
Note: The ‘with’ keyword is used along with the open() method
as it simplifies exception handling and automatically closes the
CSV file.
Example: This code reads and prints the contents of a CSV file
named ‘Giants.csv’ using the csv module in Python. It opens the
file in read mode, reads the lines, and prints them one by one
using a for loop. The csv.reader() function is used to read the CSV
file, and the data from each row is printed to the console.
import csv
# opening the CSV file
with open('Giants.csv', mode ='r')as file:
Syntax:
csv.writer(csvfile)
csv.writer class provides two methods for writing to CSV. They
are writerow() and writerows().
writerow(): This method writes a single row at a time. Field row
can be written using this method.
Syntax:
writerow()
writerows(): This method is used to write multiple rows at a
time. This can be used to write rows list.
Syntax:
writerows(rows)