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


#1

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!


#2

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:
    isbn_list.append(book['ISBN'])

#3

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.


#4

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

['978-0-12-995015-8', 
'978-1-78110-100-1', 
'978-0-315-25137-3', 
'978-0-388-70665-7', 
'978-1-75098-721-6', 
'978-1-06-483628-6', 
'978-0-7419-8114-1', 
'978-1-4457-0480-7', 
'978-0-657-61030-2', 
'978-1-5039-7539-2']

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)

#5

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


#6

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


#7

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.


#8

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).


#9

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


#10

What does the output look like?


#11

Author@ISBN@Title
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.


#12

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

help(open)

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)


#13

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.


#14

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.


#15

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.


#16

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.


#17

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?