FAQ: Learn Python: Files - Reading Different Types of CSV Files


This community-built FAQ covers the “Reading Different Types of CSV Files” exercise from the lesson “Learn Python: Files”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise Reading Different Types of CSV Files

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

new to code, was having trouble iterating through the proper dictionary, here’s my final solution:

import csv

with open('books.csv') as books_csv:
  books_reader = csv.DictReader(books_csv, delimiter='@')
  isbn_list = []
  for book in books_reader:

Instead of using a list comprehension, I use a for loop, too! But did you run this out? I couldn’t. And I tried the answer, it couldn’t run out either.


Add a print statement at the end to print out the isbn_list object. You should get this output…


The code sample above is what I have used and it passed back when I did this lesson.

list comprehension
import csv
with open('books.csv') as books_csv:
  books_reader = csv.DictReader(books_csv, delimiter='@')
  isbn_list = [book['ISBN'] for book in books_reader]
print (isbn_list)

How come the newline = ‘’ argument is not required in these answers even though they explicitly mention it multiple times in their examples?


One suspects it is because we are not writing to the file, but reading from it.


Right. Newline keyword argument is for reading. And what confused me was that the answer we’re supposed to give doesn’t require it. I suppose it’s because the file we’re reading does not contain any escape characters, so it’s safe to omit the Newline keyword. But I assume it’s considered a good practice to always put it there anyway, just in case, especially if we don’t know what the file looks like.


You and I are both guilty of assumptions… The blind leading the blind? It is possible there are no escape characters, as you reason, and that the delimiter says it all. I really don’t work enough with this sort of stuff so should keep my answers to myself (though I don’t mind being wrong, or only half right, so long as somebody eventually points it out).

1 Like

The following code reveals there are no escape characters, unless I’m misunderstanding how the .read() method works.


What does the output look like?


Lauren Murray@978-0-12-995015-8@“Enviornment Call, Amount Later Page Country”
Micheal Jones@978-1-78110-100-1@Rate Security Full
Alexander Carr@978-0-315-25137-3@Still Response Size
Michael Williams@978-0-388-70665-7@Position Result Five
Kathleen Ferguson@978-1-75098-721-6@Country Week Receive And Sign
Sarah Dorsey@978-1-06-483628-6@Audience Truth Small
Mary Middleton@978-0-7419-8114-1@Travel: Special Offer Near Allow Goal
William Todd@978-1-4457-0480-7@Money Exactly Drop Teach
Joan Martin@978-0-657-61030-2@Theory Do Half Change
Gary Roman@978-1-5039-7539-2@Bill Serve Pull Industry South Job

Bur forget what I said. If you use .readlines() it shows the \n characters, so I still can’t figure it out.


open’s newline parameter is described in open’s docstring, which you can view with:


After which you can decide whether or not you want to change the default value for that.

(Or read the docs for open at docs.python.org, specifically at https://docs.python.org/library/functions.html#open)


The documentation doesn’t specify the default value for the newline argument though. So it’s not clear to me what the code actually does when it is not passed the newline = [blank] argument.


Default values are shown in the signature
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Reading the csv module’s documentation (which you arguably should to be able to use it at all) explains why the newline argument is included - the csv module’s documentation specifically mentions it

As for why the csv module wants it that way I probably don’t even want to know, csv files are a mess almost by definition (multiple standards, or perhaps more accurately multiple non-standards)
I suppose the reason for this module existing at all is that they are a mess.

1 Like

Ahhh sorry I’m blind. I was using ctrl+f on the word “default” hah Thank you very much, that clarifies it all. The Codecademy tutorial should explain it more clearly.


Well, fine, I do know why it wants it that way.
It is so that the csv module gets control over those newlines rather than allowing open’s text mode to do “the right thing” - presumably other tools flip out because they have borderline bugs in them and can’t deal with newlines correctly. If you’re not using such tools then you’re not going to notice the difference.

Uh. Well, I guess you might get weird things like '\r\r\n' if not giving the csv module control over newlines on a windows machine. The whole thing really just makes me want to run away.


Anyone else having issues with answers being offset from instructions by 1? At instruction 2, I have to define a variable under the with/as line in order to move past, despite the instructions only being to open the .csv file and save it as a variable. That’s asking for a single line, ya?


To pass question 3 you have to do this…
(the space in “, delimiter=’@’” matters)

import csv

with open('books.csv') as books_csv:
  books_reader = csv.DictReader(books_csv, delimiter='@')

To pass question 4 you have to do this…
(remove the delimiter)

import csv

with open('books.csv') as books_csv:
  books_reader = csv.DictReader(books_csv)
  isbn_list = [book['ISBN'] for book in books_reader]


Doesn’t seem like its finding the answers to the question correctly?


This particular one was annoying. I couldn’t get past the 4th task, a key error : ISBN would keep popping up.

I ran a print (xyz.read()) on the file and noticed there were no ‘@’ present within the file, only commas were present.

I changed the delimiter from @ to a ,. This actually worked. I’m guessing because using @ as a delimiter when it was clearly not present in the file confused python and it couldn’t identify 'ISBN 'as a key, or any other key. I feel like the source file changed in recent times but the questions weren’t updated.


The 4th question is wrong. It currently is:

Create a list called isbn_list, iterate through books_csv to get the ISBN number of every book in the CSV file. Use the [‘ISBN’] key for the dictionary objects passed to it.`

and it should say

Create a list called isbn_list, iterate through books_reader to get the ISBN number of every book in the CSV file. Use the [‘ISBN’] key for the dictionary objects passed to it.

Note the change to the dictionary that needs to be iterated through.

This is related to the following exercise: