Python Files Lesson Code Submission Errors

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 ( 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:

1 Like

I copy the solution, reset the exercise and paste it, but it keeps giving me errors… there’s something wrong here, guys!

I’m having trouble on the 3rd step: Create a DictReader instance that uses the @ symbol as a delimiter to read books_csv . Save the result in a variable called books_reader .
This is the code I have inside my with, and it is not going through:

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

I thought so too at first, but suspect they use this wording to keep users on their toes. We are still iterating through the same original csv file so its technically correct, no? Just ambiguous.

There’s a bug in this one.

  • To get through step 3, you must have delimiter='@' (with NO whitespace!) in the call to csv.DictReader()

  • But then Step 4 throws KeyError: 'ISBN'

  • Eliminating the delimiter allows Step 4 to run, and adding a print() statement shows the expected output (dictionaries with appropriate keys and values).

… BUT the “Solution” includes the delimiter, and runs without throwing an error, also showing the expected output!

Go figure! Does calling “Solution” bring in a different .csv file?


whats your error code

I’m keep getting wrong file,

with open(‘books.csv’) as books_csv:

when I run code above, I get these lines,

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

and when I press the Solution button, then, the code above gives me correct ‘@’ seperated values file… why do I get comma seperated values file insted of ‘@’ seperated values file?

I tried to bypass this problem by overwriting the file

import csv

with open('books.csv', 'w') as books_csv:
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''')

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

and this is still not working… I don’t understand. why this would not work?

It could be that DictReader is parsing the lines to separate out any entries with commas and wrapping them with quotes, then replacing all the delimiters with commas. Just a thought.

I have tried ‘Reset Exercise’ and only ‘import’, ‘with’, ‘open’, ‘print’, ‘read’ was excuted.

ps. just tried without import.
Even without csv library, I’m still getting the wrong file.

pps. I usually use ff 68.0.1 (portable version), and tried edge 42.17134.1.0 too

We may have to wait until someone reports the same problem. Try to get the solution so you can move on, but bookmark that page so you can go back to it later.

Thank you,
I’m studying with a friend. I’ll see if his environment & account works in monday, may come back and report same problem.
have a good day!

1 Like

Because course authors think regex matching on user code is a good idea (spoiler: it’s not) and therefore accept only delimiter='@' and not delimiter = '@'
And then the error message doesn’t match what was actually wrong because it tests one thing and complains about another.
And, maybe someone thought csv needed to be separated by commas and then changed the file and nothing else.

The solution wants you to use list comprehension, but if you don’t it doesn’t seem to want to work properly. This code does not work, for whatever reason:

import csv

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

The code still outputs what is expected.

['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']

Is it just that it won’t pass the SCT?

I was getting a KeyError on ISBN, but oddly I went back to copy/paste the error to share here, and now it’s working. Who knows. I searched SCT that you mentioned and I guess it’s a bug that can happen in-browser sometimes. Oh well - I mimicked an earlier example in the lesson so I’m confident it would have worked but just got a buggy error somehow. Thanks

1 Like

I trust you found that it is short for, Submission Correctness Test.