DEV Community

Cover image for How to Use Python Csv Module
Tifey_
Tifey_

Posted on • Edited on

How to Use Python Csv Module

How to Use Csv Module with Csv files in Python

Csv file which means Comma separated values files. It is a type of file with extension ".csv" which is used as a spreadsheet i.e it is embedded with data in a grid of rows and columns.

When time comes for you to use a Csv file or a spreadsheet in your program. This article will be of great help as you will be able to open and work with csv files, know what modules are used for csv files, modify and work with csv as if they were they were yours.

Let's get started!!!

Python as a high level programming has a pre built module for csv files which is called "csv".
Importing the Csv Module
Since the module is already prebuilt you will only import else you will need to install
        import csv

Opening the csv file using the with statement.
The with statement is preferred by most Python developers  because it automates the close() function after using its open().
The with statement

with open ("notes.csv") as csv_file:
    data=csv.reader(csv_file)
open() : is a python function used to open files in a main.py
csv_file : is a variable which stores the file that is been open
data : is a variable assigned to the information extracted from the csv_file after its been accessed by the csv module.
.reader() : is a csv function for reading the content of a opened csv file.

N:B the reader() is read only. If you print data variable at this stage, it will generate numbers and code which represents the location of the csv file on the computer ROM.

Converting a csv file to List and Dictionary

After the file is been read by the csv module. You could start making your modifications. You can convert  it to either a list or dictionary. We will walk you through the two  conversions.

  1. Converting csv file to a list.

  2. Create an empty stored in a variable.
    List  = []

  3. Loop through the data( variable which you use to store the content of the file when the reader() was used)

  4. Append each item in the data to your new list.

Your codes should be in the below format

When the above code is run. It will outpu the  list of items in the csv file beginning from the first row and convert each row to a list type object. It will output the below

With this, you can work with your csv files like you do with your list objects.

  1. Converting a csv file to Dictionary. A dictionary works with its keys and values. To convert a csv file into a dictionary, we will need a key and value.

In this article, we will use an item in the file as the key and the other as its value since it is of two columns.

  1. Create an empty dictionary and assign it to a variable.
  2. Loop through the data
  3. Add the key and value to the empty dictionary. Your codes should be in the below format:

The key used in the above is the item at index 0 on each row of the Csv file and its value is that at index 1.
The following would be its output

Also with the above out you can also treat your Csv file like that of a dictionary. For example you could modify its value,  its key, change the values and keys, and other dictionary features.

<|||||||||||-----------------|||||||||||||>

Now let's advance a little.
How will it be if we could make all letters in the csv file become a value to a key called Letters and do likewise to the words too? It sound nice right?.

Steps

  1. Create a new empty list for the letters
  2. Use a for loop to append all the letters to the new list excluding the "letter" string It should be like the below

**The if statement in the above is used to exclude the letter string from the list
Char = a loop variable
Char[0] = the item at index 0 of Char variable

  1. Create another empty list with a variable words
  2. Use a for loop to append all the word to the words list excluding the "words" string.
    Your codes should be like below

  3. Create an empty dictionary called dict.

  4. Add a key called Letters to the empty dict and makes it value to be the Letters list object

  5. Add another key called Words to the dict and makes it value to be words list object.
    You code should be like the following

Following the above steps, you have completed the task. If you print the dict object you will get what task we want.

It's result will be:

The letters have now become the value of Letters and the words is now the value of Words.

It is very obvious that it took many lines of code to carry out a mini task using the python csv module.

Another module which python programmers use when the come across csv files is a module called  Pandas

Pandas is a module used to work with csv files. It allows programmers to work with the csv more efficiently.
Using Pandas mores task can be accomplished,  you can get the statistical information of a csv file like the mean, median, mode, standard deviation,  list conversion,  etc using the built functions.
Pandas is occupies a wide range when working with csv files.

In our next article,  we will explain more about pandas

Kindly comment !!!!!

Top comments (0)