The csv Module for Reading and Writing CSV Files in Python


Read from and write to CSV files in a straightforward manner.

Source Code

import csv
# Writing to a CSV file
with open('output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(["Name", "Age"])
    writer.writerow(["Alice", 24])
# Reading from a CSV file
with open('output.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments