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

Read CSV Records as Lists (Solution)

00:00 Okay, I have the instructions all ready with the expected output of the correct solution. To read a CSV file in Python, you can use the same csv module that you used before to create the file.

00:12 And since you’re working with files, you also need to import pathlib’s Path object, which lets you specify the path to the CSV file with numbers, which is located in your home directory.

00:24 And the file was named numbers.csv. You’re going to open the file for reading. In fact, you could skip the mode parameter because Python always opens a file for reading by default, but it never hurts to be explicit about your intent when writing code.

00:42 Okay, you can now create the corresponding CSV reader object by wrapping the file instance.

00:49 So far so good, but how do you actually read the CSV data from the file? This reader object is an interesting one because it doesn’t provide any method. So the only way to read the rows from a CSV file with it is by iterating over the reader object using a for loop.

01:08 So for every row in the reader, you can print the row to see what comes back.

01:17 Okay, I can save the file as exercise_02_02 and run it. The reader object returns the subsequent rows from the CSV file as Python lists consisting of the comma-separated values.

01:34 Notice that each number loaded from the file is represented here as a Python string, which you can tell by the presence of the single quotes around them.

01:44 The CSV format doesn’t specify any data types of its own, so every value is considered a string. It’s up to you what Python data type you want to map them to.

01:55 In this case, you are asked to treat those values as integers. There are a couple of ways to do this, but my favorite one involves using a list comprehension, which looks somewhat similar to the standard list literal in Python.

02:10 To apply one, you must use the square bracket syntax around the list that you want to iterate over, which is this row here. Then you must define a for loop inside the square brackets, and then at the front, you must include an expression that will be used to fill the new list.

02:27 In this case, you want to retain the original value, but you also want it to be an integer.

02:34 When I save the file and run it again, the quotes around the numbers disappear, indicating they’re integer literals. Now that’s great, but instead of printing the rows, we want to add them to a list.

02:47 So let me declare a variable named numbers, initialized with an empty list that we’ll populate with data later.

02:55 Now I can store the row with integer numbers in another variable called row_int, for example, and finally, I can append it to the list of numbers.

03:10 This means we won’t see the output anymore after running the script. So the last step will be printing all the numbers that we loaded from the CSV file. We want to call the print() function after leaving the with statement’s code block on the same level as the variable definition.

03:29 The output that we’re getting now is consistent with the example given in the instructions. So we’ve just completed another exercise.

Become a Member to join the conversation.