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?
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?
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?
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)
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]
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…
I do have a slightly different question when talking about csv files. What is the difference between the following code:
isbn_list = [book[“ISBN”] for book in books_reader]
isbn_list =
for book in books_reader:
isbn_list += book[“ISBN”]
Both of these codes look identical. The top one is a list comprehension vs, the bottom one is a list iteration. The bottom one iterates each and every character in books_reader. Why does it do that?? Maybe I am answering my own question, but I just need some clarification.