In this Python tutorial, we will discuss how to read csv file in Python and how to write to a csv file in Python.
What is a CSV file in python?
In python CSV is Comma Separated Values, it is a plain text file which is used to arrange the tabular data. CSV performs data interchange and it must be saved with.CSV file.
Python CSV module
In python, we will use a CSV package which is a part of the standard library, so we have to install this CSV module first. Here is how you can import CSV in python.
import csv
Python Read CSV File
- In your visual studio code, click on the file and create a file with .csv file extension in order to represent the CSV file and then save that file.
- Store the data with the following entries on the file. The element of the CSV files is separated by commas. In the below example you can see the data stored in a file.
Example:
sam@gmail.com,sammy,33
john@dey.com,john,43
stephan@cook.com,stephan,45
You can refer to the below screenshot to store data
- In python, for reading the data from the CSV file we will use CSV.reader() function. The CSV file opened as a text file by python built-in open() which returns a file object and then it is iterated using the for loop to print. Here we opened the datafile.csv in reading mode.
import csv
with open("datafile.csv", 'r') as csvfile:
rows = csv.reader(csvfile)
for row in rows:
print(row)
After writing the above code (python Read CSV file), Ones you will print ” row “ then the output will appear as a “ [‘sam@gmail.com’, ‘sammy’, ’33’] [‘john@dey.com’, ‘john’, ’43’] [‘stephan@cook.com’, ‘stephan’, ’45’] ”. Here, each row is returned by the reader is a list of string elements.
You can refer to the below screenshot for python Read CSV File.
Python Write CSV File
In python, for writing the CSV file we can use the csv.writer() function and it will iterate the data over the row. The csv.writer() function returns a writer object which will convert the data into a delimiter string, and we opened the file in writing mode. Here, when we run the program then the datafile.csv file is created.
import csv
with open("datafile.csv", 'w') as csvfile:
writer = csv.write(csvfile)
writer.writerow(['sam@gmail.com', 'sammy', '33'])
writer.writerow(['john@dey.com', 'john', '43'])
writer.writerow(['stephan@cook.com', 'stephan', '45'])
After writing the above code (python Write CSV file), Ones you will “write” then the output will appear as a “ sam@gmail.com, sammy, 33 john@dey.com, john, 43 stephan@cook.com, stephan, 45 ”. Here, datafile.csv is created with the content and the lists are converted to a delimited string and it is written into the CSV file.
You can refer to the below screenshot for python Write CSV File.
You can refer to the below screenshot after running the above program, a datafile.csv file is created with the content.
You may like the following Python tutorials:
- Function in Python
- Python Array with Examples
- Hash table in python
- Python get filename from the path
- Python For Loop with Examples
- Python if else with examples
- Python read excel file and Write to Excel in Python
- Create a tuple in Python
- Python Tkinter Entry
- Python format number with commas
- Python read a file line by line example
In this tutorial, we learned how to read CSV File in Python, and also we have seen how to Write in a CSV File in Python.
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.