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

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

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.

1 Like

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)
1 Like

12 posts were split to a new topic: Understanding the newline argument

20 posts were split to a new topic: Python Files Lesson Code Submission Errors

Hi mtf,

I have a question, you have got in your output as separed lines like this:
[‘978-0-12-995015-8’,
‘978-1-78110-100-1’,
‘978-0-315-25137-3’,
‘978-0-388-70665-7’,…
]
my questions is, why I’ve got in one single line?
[‘978-0-12-995015-8’, ‘978-1-78110-100-1’, ‘978-0-315-25137-3’, ‘978-0-388-70665-7’,…]

Thanks for you help.

Whitespace is optional, and largely ignored if the syntax is correct.

[
    value,
    value,
    value,
    value
]

is easier to read than a single line. Either form is valid.

I ran into the same issue. The problem is the instructions say to use @ as the delimiter but the actual CSV file is delimited by commas. I used to code below to take a look at the CSV and that’s how I figured it out.

print(books_csv.reader())

I copy and pasted the results from the lesson here:

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

getting ERROR all the time till I change the delimiter to ‘,’

Hey, I tried to run this code:

import csv
isbn_list = []
with open("books.csv") as books_csv:
  books_reader = csv.DictReader(books_csv, delimiter='@')
  for each in books_reader:
    isbn_list.append[each["ISBN"]]
    

But I’m getting an error: TypeError: 'builtin_function_or_method' object is not subscriptable

Why does the error appear here, if it doesn’t appear in case of this code:

(SOLUTION CODE):

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]

It’s really confusing why I accent elements to that list, even thought I am reffering to the very same elements in the loop.

.append() is a method, not a data structure so is not subscriptable.

list.append(each['ISBN'])

I see, thanks. So technically if I used a temporary list to hold ‘each’ and then overwritten isb_list instead of append it should work?

each['ISBN']

uses a key which is the syntax for accessing an attribute value in a dict object called each.