Handling CSV in Python

In Python, handling CSV (Comma Separated Values) files is easy using the built-in csv module.

Here’s an example of how we can write/read a CSV file:

import csv;

def csvHandling():
    N=[25,17,19,13,12,15]
    file = open("1.csv","w", newline='');
    csv_writer=csv.writer(file)
    csv_writer.writerow(N)    
    file.close()
    file = open("1.csv","r", newline='');
    csv_reader=csv.reader(file)
    for row in csv_reader:
        print("printing rows in csv")
        print(row)  
    file.close()

csvHandling();

Here we are writing the contents of a list to csv and displaying the contents by reading the csv.

Leave a Reply

Your email address will not be published. Required fields are marked *