I’m working with file in Python. The task is
- Import the
csv
module.- Open up the file books.csv in the variable
books_csv
.- Create a
DictReader
instance that uses the@
symbol as a delimiter to readbooks_csv
. Save the result in a variable calledbooks_reader
.- Create a list called
isbn_list
, iterate throughbooks_reader
to get the ISBN number of every book in the CSV file. Use the['ISBN']
key for the dictionary objects passed to it.
This is the key answer but I’m not familiar with the syntax at the last line (isbn_list).
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]
Here is my coding
import csv
with open("books.csv") as books_csv:
books_reader = csv.DictReader(books_csv, delimiter="@")
for row in books_reader:
isbn_list = row["ISBN"]
print(isbn_list)
The output of my coding is different from the answer because it’s not a list, just plain text. But I don’t understand how the last line in the answer key works.