FAQ: Learn Python: Files - Reading a CSV File

This community-built FAQ covers the “Reading a CSV File” 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 a CSV File

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!

8 posts were split to a new topic: What does the newline=" " argument do?

2 posts were split to a new topic: Why don’t we need an empty list in the exercise?

3 posts were split to a new topic: Step 2 gives an error

3 posts were split to a new topic: What does the example mean by using the third field “email” as a key in a dictionary?

5 posts were split to a new topic: How can I print my dictionary in a readable format?

What if the first line of the CSV does not contain the labels of csv fields?

If the keys of the dictionary are, by default, the entries in the first line of our CSV file what happens if the first line is data?

Would this process for csv files in python be the same as creating a data frame in pandas?

Why does the DictReader Dictionary object only work while the file is still open?

Since we saved it to a python variable, shouldn’t we be able to call the variable after we close the file, like when using read() in the previous exercises?

I know the proper way to code this would be:

with open("cool_csv.csv") as cool_csv_file:
  cool_csv_dict = csv.DictReader(cool_csv_file)
  for row in cool_csv_dict:
    print(row["Cool Fact"])

But why does the for row in cool_csv_dict: section have to be nested in the open() section?
I don’t intuitively understand why the following code returns a ValueError: I/O operation on closed file.

with open("cool_csv.csv") as cool_csv_file:
  cool_csv_dict = csv.DictReader(cool_csv_file)
  
for row in cool_csv_dict:
  print(row["Cool Fact"])

My only guess would be that because the csv.DictReader object is not quite an actual dictionary (or something), there’s some shenanigans because it still needs to point somewhere. Can anyone shed any light?

2 Likes

You’re certainly along the right lines as it doesn’t actually return a dictionary but there’s no need to guess, that’s what the docs are for- https://docs.python.org/3/library/csv.html#csv.DictReader

When do we know that we need to use newline when opening the csv file?

It’s suggested for any csv file objects (reading/writing .csv files)- csv.Reader, csv.Writer, csv.DictReader, csv.DictWriter

This lesson assumes we know what dictionaries or keys are when we haven’t covered that concept so far in the ‘Analyze Data with Python Skill Path’.

Very difficult to keep up

1 Like

Codeacademy, I request please explain things properly, don’t jump, I didn’t get why the newline=“” argument was passed in open() function along with the csv file. Before that I just thought that only w,a, and r can be passed, please explain first what arguments can be passed in open() function, then move ahead, it will be easy.

1 Like

Maybe this would help:

2 Likes

Haha…got it really keeping it up for now, otherwise it may create confusion, hope will get to know about it in this course. Thank you