DEV Community

Cover image for Python read and write csv file
Max
Max

Posted on

Python read and write csv file

In this tutorial, we will learn about how to read and write CSV file using python with examples.

CSV full form is Comma Separated Values as the name says, each value inside the file is separated by comma.

Python have the inbuild csv module using that we can perform operation in csv files. Similary to the python text read function, csv file can be read using python with. Default file mode is read r, its not required to mention if not given it will take the default mode.

Read CSV file using python example

import csv

with open(r'C:\Users\Mageshwaran\Downloads\Sample.csv', mode='r') as csvFile:
    csvData = csv.reader(csvFile)
    print("csvData:", csvData)
    for row in csvData:
        # row variable have row data as list
        print(row)
Enter fullscreen mode Exit fullscreen mode

Write CSV file using python example

Writing to CSV file is similar to read operation, here the file mode is set to write w

write one row at a time to csv file in python

import csv
with open('sample_write.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(['1', 'Eldon Base for stackable storage shelf, platinum', 'Muhammed MacIntyre', '3', '-213.25', '38.94', '35', 'Nunavut', 'Storage & Organization', '0.8'])
    writer.writerow(['2', '1.7 Cubic Foot Compact "Cube" Office Refrigerators', 'Barry French', '293', '457.81', '208.16', '68.02', 'Nunavut', 'Appliances', '0.58'])
    writer.writerow(['3', 'Cardinal Slant-D® Ring Binder, Heavy Gauge Vinyl', 'Barry French', '293', '46.71', '8.69', '2.99', 'Nunavut', 'Binders and Binder Accessories', '0.39'])
Enter fullscreen mode Exit fullscreen mode

write one row at a time to csv file using python

Write multiple rows at a time to csv file in python

In python we can convert python list of list to csv file using csv writerows methods

import csv

data = [
    ['4', 'R380', 'Clay Rozendal', '483', '1198.97', '195.99', '3.99', 'Nunavut', 'Telephones and Communication', '0.58'],
    ['5', 'Holmes HEPA Air Purifier', 'Carlos Soltero', '515', '30.94', '21.78', '5.94', 'Nunavut', 'Appliances', '0.5'],
    ['6', 'G.E. Longer-Life Indoor Recessed Floodlight Bulbs', 'Carlos Soltero', '515', '4.43', '6.64', '4.95', 'Nunavut', 'Office Furnishings', '0.37'],
    ['7', 'Angle-D Binders with Locking Rings, Label Holders', 'Carl Jackson', '613', '-54.04', '7.3', '7.72', 'Nunavut', 'Binders and Binder Accessories', '0.38'],
    ['8', 'SAFCO Mobile Desk Side File, Wire Frame', 'Carl Jackson', '613', '127.70', '42.76', '6.22', 'Nunavut', 'Storage & Organization', '']
]
with open('sample_write.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(data)
Enter fullscreen mode Exit fullscreen mode

you can see writing second time to the csv file override the previous data, this can be overcome by using the write append file method.
Write multiple rows at a time to csv file in python

Check python tutorials

Explore More Articles

Python Install Jupyter Notebook
Python Create Virtual Environment
Read and Write JSON in Python Requests from API
Read and Write Python Json
How to combine two dictionaries in python using different methods
How to check if a key exists in a dictionary python

Latest comments (0)