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

7 posts were split to a new topic: Do I need to use @ or a , for the delimiter?

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

4 posts were split to a new topic: How can I use append with each?

2 posts were split to a new topic: Responses to deleted message

Hi all,

Thank you again for reading this.

My question is about the “newline = ‘’” argument. I understand what the intention behind using that line is. I just don’t understand the mechanism behind its action. What’s between the ‘’ marks exactly?

Thank you again.

Hello all,

Thank you for reading this.

My question is about CSV files and the way we “extract” certain values from it. From the examples I’ve seen so far, it seems like the CSV files have columns (for example ‘name,’ ‘address,’ etc.). Then why is it that when we’re iterating through cvs.DictReader instance, we use the term “row” instead?

having columns doesn’t mean there are no rows

you can iterate through whatever you want, you, the programmer, are the one deciding what happens in your program

have you looked at a csv file? what would iterating through that mean? maybe you can answer your own question if you consider what’s being done instead of how it’s done

and if you iterate through a csv file that you have looked at, what do you get, do you get columns, or rows, and what would you therefore say it is?

1 Like

Thank you for your response. I think I got it.

Currently finishing up the section and I understand the solution they gave but while working on my own I can’t figure out the line of code I would need to combine the lists outputs after the for loop iterations

import csv

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

Create the empty list before the for loop, that is the only error in your code.

Here is(are) my solution(s).
I wanted to write this as a list comprehension to begin with but couldn’t quite wrap my head around the logic.
So instead I wrote a for loop first, and then reading the for loop backwards wrote the list comprehension.

First solution is commented out, list comprehension follows that.

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'])
  isbn_list = [isbn_list.append(book['ISBN']) for book in books_reader]

Nice - I’m kinda surprised that works though as you are both assigning to and appending to isbn_list at the same time. The comprehension defines the new list so you can skip the append step altogether.

All you need for that last line is:

isbn_list = [book['ISBN'] for book in books_reader]
1 Like

I don’t understand this line:

isbn_list = [book['ISBN'] for book in books_reader]

I understand that is a ‘shorter’ way to append the ISBN more directly but Can someone explain step by step?

Thanks,

Have you completed the list section that describes comprehensions?

First, let’s look at it in imperative form (instruction steps):

isbn_list = []
for book in books_reader:
    isbn_list.append(book['ISBN'])

From the above we need to assume the makeup of books_reader and book objects. The syntax suggests, book is a dictionary (dict) and books_reader is a list.

The loop takes one object off the list and appends the value of its ISBN attribute to the ISBN list. That is our objective, build a list of ISBNs from the data store.

You should be comfortable with this. Give us a thumbs up and we can go on…

1 Like

Oh my, I completely forgot the comprehension syntax. I understand now. Thank you,

1 Like