Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Read CSV Records as Dictionaries (Solution)

00:00 Reading a CSV file containing a headline into a list of dictionaries requires creating a DictReader object. But first, we need to include the usual bit of boilerplate code that we’ve been typing over and over again.

00:14 At this point, you should know the drill. We’ll start by importing the necessary modules—that is, the csv module and the Path object from pathlib.

00:25 Now we can define a Path instance pointing to the favorite_colors.csv file in the home directory. If you haven’t completed the previous exercise, then you can find the required CSV file in the supporting materials available for download.

00:41 Alternatively, you can create the file manually and copy its content from the slides. We can now open the file for reading using the with statement, remembering to set the character encoding and disable the universal newline.

01:02 The next step is to create a DictReader instance and wrap our file object with it.

01:09 But what about the headline? When creating a DictWriter object, we specify the column names through the optional fieldnames parameter.

01:17 With a DictReader object, however, things look a bit more complicated because the underlying CSV file may or may not come with a header line.

01:27 When it does, the reader object can infer the corresponding column names from the first row, assuming it’s the header line. Otherwise, you must specify the field names explicitly when creating the reader object.

01:40 When you do, the fieldnames parameter takes precedence and the headline present in the file will be treated as regular data. So you should generally know if your CSV file includes a headline or not before proceeding to avoid any confusion or problems in your data processing.

01:59 In this case, I’ll let the reader perform the inference by not specifying the column names myself because I know that our file comes with a headline. Now, the DictReader happens to be an adapter for the standard CSV reader providing the conversion between the rows and Python dictionaries.

02:18 Therefore, it has the same interface, allowing me to iterate over it. The difference is that each row is a dictionary instead of a list. So I’ll name the variable accordingly.

02:31 That’s for row in reader, and since the rows are dictionaries, I can append them to my favorite_colors.

02:51 Let’s print them out,

02:56 save the code as exercise_02_04 and run it.

03:04 The actual output agrees with what the instructions are telling us.

03:10 Okay, that was the last review exercise in this course before the final challenge. I hope that you’re ready to put all your knowledge and skills to the test.

Become a Member to join the conversation.