How do I write a CSV file?
To write data into a CSV file in Python, we use the csv.writer() function to create a writer object that can write data to the file. The writer object has methods like writerow() and writerows() that can write one or more rows of data to the file.
Program for write single row in csv file
#Program for write in csv file
import csv
fobj=open("ram.csv","w",newline='')
f=csv.writer(fobj)
data=['roll','name','marks']
f.writerow(data)
fobj.close()
Program for write single row and multiple rows in csv file
Program
#Program for write in csv file
import csv
fobj=open("ram.csv","w",newline='')
f=csv.writer(fobj)
data=['roll','name','marks']
f.writerow(data)
data2=[[1,'Devansh',99],[2,'Divyansh',88],[3,'Ajay',77],
[4,'pavan',87],[5,'abhay',76]]
f.writerows(data2)
fobj.close()
good sir
ReplyDeleteGOOD
ReplyDelete