Convert CSV to a Dictionary in Python

To use CSV files in Python, you have to import the CSV module. It offers functions that can read the file (csv.reader) or read and map it to a dictionary (csv.DictReader).

Creating a file

First, we need to create a file. You can create a text file and enter the following information about people: First Name, Last Name, and Age. The default delimiter is a comma. Save it as a CSV file.

First Name,Last Name,City,State
James,Butt,New Orleans,LA

CSV reader

The CSV reader function iterates over lines in the given CSV file. It doesn’t offer any tools that help us create a dictionary from this file. We have to deal with this ourselves.

First, we need to open a file for reading and save it to a buffered text stream (TextIOWrapper).

Lines are read from the buffer with the csv.reader function.

Each line consists of columns (in our case 4). In the first iteration of the loop, each value from the first row (header) is converted from a tuple to a list.

In the for loop, we iterate over header elements. With the enumerate function we have an index number that can be used to retrieve the element from the line list and assign it to the people_dict dictionary.

The dictionary is then appended to the people_list list, creating a list of dictionaries.

The full code looks like this:

CSV DictReader

The csv.DictReader function operates similarly to csv.reader, but in addition to just reading the stream, it maps the data from each row to a  dict. The result from the previous example can be achieved using this code, which is much more compact.

Weird character at the first header element

If you create the CSV file, sometimes you may find weird characters at the beginning of the first header element.

This can happen if you create a file in Excel and save it with incorrect character encoding.

If you want all characters to be displayed correctly, save the file using CSV (MS-DOS)(*.csv) for Windows.