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
Josephine,Darakjy,Brighton,MI
Art,Venere,Bridgeport,NJ
Lenna,Paprocki,Anchorage,AK
Donette,Foller,Hamilton,OH
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).
1 |
with open(file_path, 'r') as data |
Lines are read from the buffer with the csv.reader function.
1 |
for line in csv.reader(data) |
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.
1 |
headers_list = list(line) |
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.
1 2 |
for i, elem in enumerate(headers_list): people_dict[elem] = line[i] |
The dictionary is then appended to the people_list list, creating a list of dictionaries.
The full code looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import csv file_path = 'D:/data.csv' people_list = [] headers_list = [] index = 0 with open(file_path, 'r') as data: for line in csv.reader(data): index += 1 if index > 1: people_dict = {} for i, elem in enumerate(headers_list): people_dict[elem] = line[i] people_list.append(people_dict) else: headers_list = list(line) |
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.
1 2 3 4 5 6 7 8 9 |
import csv people_list = [] file_path = 'D:/data.csv' input_file = csv.DictReader(open(file_path)) for row in input_file: people_list.append(row) |
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.