How come the newline = ‘’ argument is not required in these answers even though they explicitly mention it multiple times in their examples?
One suspects it is because we are not writing to the file, but reading from it.
Right. Newline keyword argument is for reading. And what confused me was that the answer we’re supposed to give doesn’t require it. I suppose it’s because the file we’re reading does not contain any escape characters, so it’s safe to omit the Newline keyword. But I assume it’s considered a good practice to always put it there anyway, just in case, especially if we don’t know what the file looks like.
You and I are both guilty of assumptions… The blind leading the blind? It is possible there are no escape characters, as you reason, and that the delimiter says it all. I really don’t work enough with this sort of stuff so should keep my answers to myself (though I don’t mind being wrong, or only half right, so long as somebody eventually points it out).
The following code reveals there are no escape characters, unless I’m misunderstanding how the .read() method works.
What does the output look like?
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
Bur forget what I said. If you use .readlines() it shows the \n characters, so I still can’t figure it out.
open’s newline parameter is described in open’s docstring, which you can view with:
After which you can decide whether or not you want to change the default value for that.
(Or read the docs for open at docs.python.org, specifically at https://docs.python.org/library/functions.html#open)
The documentation doesn’t specify the default value for the newline argument though. So it’s not clear to me what the code actually does when it is not passed the newline = [blank] argument.
Default values are shown in the signature
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Reading the csv module’s documentation (which you arguably should to be able to use it at all) explains why the newline argument is included - the csv module’s documentation specifically mentions it
As for why the csv module wants it that way I probably don’t even want to know, csv files are a mess almost by definition (multiple standards, or perhaps more accurately multiple non-standards)
I suppose the reason for this module existing at all is that they are a mess.
Ahhh sorry I’m blind. I was using ctrl+f on the word “default” hah Thank you very much, that clarifies it all. The Codecademy tutorial should explain it more clearly.
Well, fine, I do know why it wants it that way.
It is so that the csv module gets control over those newlines rather than allowing open’s text mode to do “the right thing” - presumably other tools flip out because they have borderline bugs in them and can’t deal with newlines correctly. If you’re not using such tools then you’re not going to notice the difference.
Uh. Well, I guess you might get weird things like
'\r\r\n' if not giving the csv module control over newlines on a windows machine. The whole thing really just makes me want to run away.
The csv module documentation for python 3.8.0 indicates that .DictReader includes an underlying .reader instance (along with .reader’s optional arguments). The documentation for .reader states:
Return a reader object which will iterate over lines in the given csvfile . csvfile can be any object which supports the iterator protocol and returns a string each time its
__next__()method is called — file objects and list objects are both suitable. If csvfile is a file object, it should be opened with
And the footnote that follows reads:
newline=''is not specified, newlines embedded inside quoted fields will not be interpreted correctly, and on platforms that use
\r\nlinendings on write an extra
\rwill be added. It should always be safe to specify
newline='', since the csv module does its own (universal) newline handling.
So, the newline=’’ parameter should be used when the .reader or .DictReader functions are called on a file object (as opposed to being called on a list or other iterable object), to ensure that embedded newlines are interpreted correctly.
It’s anyone’s guess as to why Codecademy’s parser throws an error if you try to use it for this exercise, since this is exactly the kind of scenario that it seems to be intended for. Perhaps the exercise was designed with an older version of Python in mind…?
i need your opinion about this answer
with open(“books.csv”, newline = “”) as books_csv:
books_reader = csv.DictReader(books_csv, delimiter = ‘@’)
for item in books_reader:
Same answer as yours! It prints out the following:
[‘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’]
I have a question unrelated to this post, but concerns this lesson.
why isbn_list = [book[‘ISBN’] forbook in books_reader] works
for book in books_reader:
isbn_list = book[‘ISBN’]
is assigning a list (comprehension); whereas,
is assigning a string value.
isbn_list =  for book in books_reader: isbn_list.append(book['ISBN']
this is continuously assigning the value of ISBN instead appending the value