Python read and write csv file
Tags: python, programming, tutorial, beginners
Published: 2023-03-15 16:10:01

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
```py
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)
```

## 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
```py
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'])
```

![write one row at a time to csv file using python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/weskr0ilidxjzrsp9nhm.png)

### Write multiple rows at a time to csv file in python
In python we can convert [python list of list](https://www.programdoc.com/python/lists) to csv file using **csv writerows methods**
```py
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)
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3horwcqaataweobs48g2.png)

Check [python tutorials](https://www.programdoc.com/python/intro)

### Explore More Articles
[Python Install Jupyter Notebook](https://dev.to/max24816/install-jupyter-notebook-1cm0)
[Python Create Virtual Environment](https://dev.to/max24816/python-create-virtual-environment-3iai/edit)
[Read and Write JSON in Python Requests from API](https://dev.to/max24816/read-and-write-json-in-python-requests-from-api-52ga)
[Read and Write Python Json](https://dev.to/max24816/read-and-write-python-json-h6i)
[How to combine two dictionaries in python using different methods](https://dev.to/max24816/how-to-combine-two-dictionaries-in-python-using-different-methods-3hcc)
[How to check if a key exists in a dictionary python](https://dev.to/max24816/how-to-check-if-a-key-exists-in-a-dictionary-python-132i)