How to create CSV file in Python
A CSV file (Comma Separated Values) is a type of plain text file that uses specific structuring to arrange tabular data.
In python, the csv library implements classes to read and write tabular data in CSV format.
Create a CSV file means to write to a CSV file in Python. The csv.writer()
function returns a writer
object that converts the user’s data into a delimited string. This string can later be used to write into CSV files using the writerow()
function.
import csv #import csv module to work with csv with open('Student.csv', 'w', newline='') as file: #opening the file in write mode writer = csv.writer(file) writer.writerow(["Name", "Age", "Rollno"]) writer.writerow(["Raju", 21, 1]) writer.writerow(["Kate", 20, 2]) writer.writerow(["Mary", 20, 3])
The file Student.csv will be created in the same directory where you have saved the above code. The file will be as follows.
Subscribe
Login
Please login to comment
0 Discussion