Python Read CSV File and Write CSV File

By: On:

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:

[email protected],sammy,33
[email protected],john,43
[email protected],stephan,45

You can refer to the below screenshot to store data

Python Read CSV File
  • 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 “ [‘[email protected]’, ‘sammy’, ’33’] [‘[email protected]’, ‘john’, ’43’] [‘[email protected]’, ‘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 Read CSV File
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(['[email protected]', 'sammy', '33'])
writer.writerow(['[email protected]', 'john', '43'])
writer.writerow(['[email protected]', 'stephan', '45'])

After writing the above code (python Write CSV file), Ones you will “write” then the output will appear as a [email protected], sammy, 33 [email protected], john, 43 [email protected], 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.

Python Write CSV File
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.

Python Write CSV File
Python Write CSV File

You may like the following Python tutorials:

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.

Leave a Comment