Before discussing about "CSV Read and Write", We have to know what is CSV? Please refer the below link to know more about CSV.
In python, if we want to work with CSV files we have to import CSV module
CSV Write:
import csv
file = open("sample.csv", "w", newline = "") # First we have to open the file
csvwrite = csv.writer(file) # Creating the object
csvwrite.writerow(["Name", "Age", "Occupation"]) # Write row method will take only one argument
csvwrite.writerow(["Alex", "25", "Worker"]) # We have to separate the values with comma
Output:
CSV Read:
import csv
file = open("sample.csv", "r")
csvread = csvreader(file)
for column in csvread:
print(column)
Output:
['Name', 'Age', 'Occupation']
['Alex', '25', 'Worker']