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

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 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?

2 Likes

whats your error code